Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 查找OSMdroid中的当前位置_Android_Osmdroid - Fatal编程技术网

Android 查找OSMdroid中的当前位置

Android 查找OSMdroid中的当前位置,android,osmdroid,Android,Osmdroid,我想通过在OSMdroid中显示图标来查找我当前位置的地图,并在移动时进行更新。在给出具体位置纬度和经度以及绘图图标时,我发现没有问题,但在给出当前位置纬度和经度时有两个错误 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext

我想通过在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();        
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy=newdefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.main);
mMapView=(MapView)this.findViewById(R.id.MapView);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(真);
mMapView.setMultiTouchControls(真);
mapController=this.mMapView.getController();
mapController.setZoom(15);
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
标准。setAltitudeRequired(false);
标准。需要设置(假);
条件.setCostAllowed(true);
标准。设置功率要求(标准。功率低);
字符串提供程序=mLocMgr.getBestProvider(条件为true);
Location Location=mLocMgr.getLastKnownLocation(提供者);
GeoPoint mypoint=新的地理点(location.getLatitude(),location.getLatitude());//此处为中心地图
GeoPoint mypointicon=新的地理点(location.getLatitude()+1000,location.getLatitude()+1000);//图标位于此处
mapController.setCenter(mypoint);
mLocMgr=(位置管理器)getSystemService(位置服务);
mLocMgr.RequestLocationUpdate(LocationManager.GPS_提供程序,1000100,
这),;
ArrayList items=新建ArrayList();
添加(新的覆盖项(“此处”,“示例说明”,mypointicon));
this.mMyLocationOverlay=新的itemizedictionoverlay(items,
新的itemizedictionoverlay.onitemgestruelistener(){
@凌驾
公共布尔值单标签(最终整数索引,
最终覆盖项目){
Toast.makeText(
MapOverLayGiveActivity.this,
“Item'”+Item.mTitle,Toast.LENGTH_LONG.show();
返回true;//我们“处理”了此事件。
}
@凌驾
公共布尔值(最终整数索引,
最终覆盖项目){
Toast.makeText(
MapOverLayGiveActivity.this,
“Item'”+Item.mTitle,Toast.LENGTH_LONG.show();
返回false;
}
},mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
}
错误: java.lang.NullPointerException
java.lang.RunTimeException


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

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

好吧,假设你没有互联网或全球定位系统。在我看来,在地图上有一个默认点是安全的。 在这段代码中,我检查一个有效的位置。如果为null,则使用默认值。 在我的申请中我没有问题。即使我的位置改变了,我也会在地图上画一条导航路径

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

}

要跟踪位置更改,请执行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);
    }       
}

如果您在Emulator上进行测试,请记住使用DDMS设置当前的lat和long。如果不这样做,getLastKnownLocation将返回null

我在上面写了一个答案。请查看是否适合您。实际上,我不想指定纬度和经度(MAP_DEFAULT_latitude=44.445883 MAP_DEFAULT_latitude=26.040963),但OSMdroid将采用当前位置的纬度和经度。我该如何做?