2012-03-04 35 views
0

的並沒有出現,而不是指定位置重疊
改變疊加的,並從onCreat()方法來updateOverlays(),我只是想獲得正確的位置地圖代碼,並設置疊加層的正確地圖無法在地圖上顯示我的位置?

public class tabsActivity extends MapActivity 
    { 
    private static final String LIST_TAB_TAG = "Notification"; 
    private static final String MAP_TAB_TAG = "Map"; 

    private TabHost tabHost; 
    private ListView listView; 
    private MapView mapView; 
    MyLocationOverlay Compass; 
    double longitude , latitude; 
    MapController mc; 
    GeoPoint point; 

    protected LocationManager locationManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.maps_notification_tabs); 

     LocationListener listener = new LocationListener() 
     { 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) 
      { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProviderEnabled(String provider) 
      { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProviderDisabled(String provider) 
      { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onLocationChanged(Location location) 
      { 
       updateOverlays(location); 
      } 
     }; 

     LocationManager locMgr = (LocationManager) getBaseContext().getSystemService(Context.LOCATION_SERVICE); 
     locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 

     // setup map view 
     mapView = (MapView) findViewById(R.id.mapview); 


     //Compass Setup 
     Compass = new MyLocationOverlay(this, mapView); 
     mapView.getOverlays().add(Compass); 
     final MapController control = mapView.getController(); 



    } 

    public void updateOverlays(Location location) 
    { 
     mapView.setBuiltInZoomControls(true); 
     mapView.postInvalidate(); 
     mapView.setSatellite(true); 
     mc = mapView.getController(); 

     point = new GeoPoint((int) location.getLatitude() , (int) location.getLongitude()); 
     mc.animateTo(point); 
     mc.setZoom(10); 
     mapView.invalidate(); 

     OverlayItem overlayitem = new OverlayItem(point, "Hint", "Your Are Here"); 

     List<Overlay> mapOverlays = mapView.getOverlays(); 
     Drawable drawable = this.getResources().getDrawable(R.drawable.black); 
     MapOverlays itemizedoverlay = new MapOverlays(drawable, this); 
     itemizedoverlay.addOverlay(overlayitem); 
     mapOverlays.add(itemizedoverlay); 
    } 
+0

感謝編輯:) @Rahul GARG – Mahmoud 2012-03-04 12:59:25

+0

的問題是如何好是你的GPS定位?你有沒有檢查準確性?多少「顯示錯誤」?錯誤的國家?還是隻有幾百米? – WarrenFaith 2012-03-04 13:01:26

+0

我的gps在谷歌地圖上運行良好,它的錯誤國家 – Mahmoud 2012-03-04 13:25:43

回答

1

你問題很容易解釋:您只使用您的程序仍在onCreate()方法中的位置。

您需要從偵聽器更新您的疊加層。因此,請創建一個可從LocationListener中調用的獨立方法來更新疊加層。

編輯:(!沒有完成,但應該給你的想法)

基本上做到這一點

public class MyMapActivity extends MapActivity { 
    MapView mapView; 
    LocationListener locListener; 

    public onCreate() { 
     // setup your map 
     mapView = findViewById(R.id.my_map); 
     // setup listener 
     locListener = new LocationListener() { 
      // override methods 
      public void onLocationChanged(Location location) { 
       updateOverlays(location); 
      } 
     } 
    } 

    public void updateOverlays(Location location) { 
     // this is basically your code just a bit modified (removed unnecessary code and added new code) 
     mc = mapView.getController(); 
     p = new GeoPoint(location.getLatitudeE6(), location.getLongitudeE6()); 

     mc.animateTo(p); 
     mc.setZoom(10); 
     mapView.invalidate(); 

     // remove all existing overlays! 
     List<Overlay> mapOverlays = mapView.getOverlays(); 
     mapOverlays.clear(); 

     //Compass Setup 
     Compass = new MyLocationOverlay(this, mapView); 
     mapOverlays.add(Compass); 

     Drawable drawable = getResources().getDrawable(R.drawable.black); 
     MapOverlays itemizedoverlay = new MapOverlays(drawable, this); 

     OverlayItem overlayitem = new OverlayItem(p, "Hint", "Your Are Here"); 
     itemizedoverlay.addOverlay(overlayitem); 
     mapOverlays.add(itemizedoverlay); 
    } 
} 

EDIT2:

您的GeoPoint的計算錯誤。你不給1E6整數,你只是給一些小的雙打。更改

point = new GeoPoint((int) location.getLatitude() , (int) location.getLongitude()); 

point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); 
+0

編輯我的答案 – WarrenFaith 2012-03-04 13:59:16

+0

我做了你告訴我的,但他們是一個例外 無法實例化活動componentinfo java.lang.nullpointerexception – Mahmoud 2012-03-04 16:02:36

+0

看看NPE被拋出的行,確定它的原因。 NPE很容易修復... – WarrenFaith 2012-03-04 16:49:53

相關問題