在OSMdroid中查找当前位置

4

我希望能够通过在OSMdroid中显示一个图标来找到我的当前位置的地图,并在移动时更新它。当我提供特定的位置纬度和经度并绘制图标时,我没有发现任何问题,但是当我提供当前位置的纬度和经度时出现了两个错误。

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
    setContentView(R.layout.main);        
    mMapView = (MapView) this.findViewById(R.id.mapview);
    mMapView.setTileSource(TileSourceFactory.MAPNIK);
    mMapView.setBuiltInZoomControls(true);
    mMapView.setMultiTouchControls(true);        
    mapController = this.mMapView.getController();
    mapController.setZoom(15);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = mLocMgr.getBestProvider(criteria, true);
    Location location = mLocMgr.getLastKnownLocation(provider);
    GeoPoint mypoint = new GeoPoint(location.getLatitude(), location.getLongitude()); // centre map here
    GeoPoint mypointicon = new GeoPoint(location.getLatitude()+1000, location.getLongitude()+1000); // icon goes here
    mapController.setCenter(mypoint);            
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
            this);        
    ArrayList<OverlayItem> items=new ArrayList<OverlayItem>();
    items.add(new OverlayItem("Here", "Sample Description", mypointicon));
    this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
            new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                @Override
                public boolean onItemSingleTapUp(final int index,
                        final OverlayItem item) {
                    Toast.makeText(
                            MapOverLayGiveActivity.this,
                            "Item '" + item.mTitle, Toast.LENGTH_LONG).show();
                    return true; // We 'handled' this event.
                }
                @Override
                public boolean onItemLongPress(final int index,
                        final OverlayItem item) {
                    Toast.makeText(
                            MapOverLayGiveActivity.this, 
                            "Item '" + item.mTitle ,Toast.LENGTH_LONG).show();
                    return false;
                }
            }, mResourceProxy);
    this.mMapView.getOverlays().add(this.mMyLocationOverlay);
    mMapView.invalidate();        
}

错误: java.lang.NullPointerException java.lang.RunTimeException
我的代码有什么问题?我该怎么做?谢谢...

我在这个帖子上写了一个答案,请看看是否对您有用。 - Ahyle Blue
实际上,我不想指定纬度和经度(MAP_DEFAULT_LATITUDE = 44.445883 MAP_DEFAULT_LONGITUDE = 26.040963),但是OSMdroid将采用当前位置的纬度和经度。我该怎么做? - Tauhidul Alam
3个回答

6
为了跟踪位置变化,请按照以下方式实现LocationListener:
public class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        currentLocation = new GeoPoint(location);
        displayMyCurrentLocationOverlay();
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

在onCreate方法内部:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationListener = new MyLocationListener();        
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if( location != null ) {
        currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude());
    }
}

并显示我的当前位置覆盖层方法:

private void displayMyCurrentLocationOverlay() {
    if( currentLocation != null) {
        if( currentLocationOverlay == null ) {
            currentLocationOverlay = new ArrayItemizedOverlay(myLocationMarker);
            myCurrentLocationOverlayItem = new OverlayItem(currentLocation, "My Location", "My Location!");
            currentLocationOverlay.addItem(myCurrentLocationOverlayItem);
            mapView.getOverlays().add(currentLocationOverlay);              
        } else {
            myCurrentLocationOverlayItem.setPoint(currentLocation);
            currentLocationOverlay.requestRedraw();
        }
        mapView.getController().setCenter(currentLocation);
    }       
}

1

如果您在模拟器上进行测试,请记得使用DDMS设置您当前的纬度和经度。否则,getLastKnownLocation将返回null。


1

您可以使用this.myLocationOverlay - 这样osmDroid就会绘制当前位置,但是为了更新位置,您必须使用位置监听器。此外,您必须使用mapView.getOverlays.clear()函数从地图中删除先前的覆盖层。

好吧,假设您无法访问互联网或GPS。在我看来,在地图上设置默认点是安全的。 在这段代码中,我检查有效位置。如果为空,则使用默认值。 在我的应用程序中,我没有遇到任何问题。即使我的位置发生变化,我也会在地图上绘制导航路径。

{
    Location location = null;

            for (String provider : locationManager.getProviders(true))
            {
                location = locationManager.getLastKnownLocation(provider);
                if (location != null)
                {
                    //location.setLatitude(MAP_DEFAULT_LATITUDE);
                    //location.setLongitude(MAP_DEFAULT_LONGITUDE);
                    locationManager.requestLocationUpdates(provider, 0, 0, mUpdateHandler);
                    break;
                }
            }

            //add car position
            if (location == null)
            {
                location = new Location(LocationManager.GPS_PROVIDER);
                location.setLatitude(MAP_DEFAULT_LATITUDE);
                location.setLongitude(MAP_DEFAULT_LONGITUDE);
                updateCarPosition(new GeoPoint(location));
            }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接