【问题标题】:find out current position in OSMdroid找出 OSMdroid 中的当前位置
【发布时间】:2012-05-15 09:59:23
【问题描述】:

我想通过在 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

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

【问题讨论】:

  • 我在post 上写了一个答案。请看看你是否可以。
  • 其实我不想指定纬度和经度(MAP_DEFAULT_LATITUDE = 44.445883 MAP_DEFAULT_LONGITUDE = 26.040963)但是OSMdroid会采用当前位置的纬度和经度。我该怎么做?

标签: android osmdroid


【解决方案1】:

要跟踪位置变化,请按如下方式实现 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);
    }       
}

【讨论】:

    【解决方案2】:

    您使用 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));
                }
    
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多