找出OSMdroid中的当前位置

时间:2012-05-15 09:59:23

标签: android osmdroid

我想找到我当前位置的地图并在我移动时通过在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

我的代码出了什么问题,我该怎么做?感谢...

3 个答案:

答案 0 :(得分: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);
    }       
}

答案 1 :(得分:1)

你使用this.myLocationOverlay - 因此osmDroid绘制当前位置 - 但是为了更新位置,你必须使用位置监听器。此外,您必须使用mapView.getOverlays.clear()函数

从地图中删除以前的叠加层

好吧,假设您无法访问互联网或GPS。在我看来,在地图上有一个默认点是安全的。 在这段代码中,我检查了一个有效的位置。如果为null,则使用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));
            }

}

答案 2 :(得分:1)

如果您在模拟器上进行测试,请记住使用DDMS设置当前的lat&amp;长。如果不这样做,getLastKnownLocation将返回null