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

我的代码有什么问题,我该怎么做?谢谢...

4

3 回答 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());
    }
}

和 displayMyCurrentLocationOverlay 方法:

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);
    }       
}
于 2012-07-25T10:04:27.523 回答
1

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

于 2012-12-29T09:29:47.327 回答
1

您使用 this.myLocationOverlay - 因此 osmDroid 绘制当前位置 - 但要更新位置,您必须使用位置侦听器。mapView.getOverlays.clear()此外,您必须使用函数从地图中删除以前的叠加层

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

{
    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));
            }

}
于 2012-05-15T21:56:26.650 回答