2012-05-15 107 views
3

我想查找當前位置的地圖並在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

什麼是錯在我的代碼,我怎麼能做到這一點?謝謝...

+0

我寫了這個[文章](http://stackoverflow.com/questions/10575285/determine-user-location-on-osm-maps/10585267#答案10585267)。請看看它是否適合你。 –

+0

實際上我不想指定經度和緯度(MAP_DEFAULT_LATITUDE = 44.445883 MAP_DEFAULT_LONGITUDE = 26.040963),但OSMdroid會採用當前位置的緯度和經度。我可以做到嗎? –

回答

1

您使用this.myLocationOverlay - 因爲osmDroid繪製當前位置 - 但爲了更新位置,您必須使用位置偵聽器。另外,您必須使用mapView.getOverlays.clear()功能刪除之前的覆蓋圖功能

好吧,讓我們假設您沒有訪問Internet或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)); 
      } 

} 
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); 
    }  
} 
+0

完美!!!,謝謝.... – TharakaNirmana

1

如果你測試上Emula然後記住使用DDMS來設置您當前的經緯度。如果你不這樣做,getLastKnownLocation將返回null