0

我試圖實施Google定位應用程序,以顯示用戶在Google地圖上的位置。我在堆棧溢出中發現了這個代碼,但是這個代碼的問題是,它顯示距離我的正確位置500米或1000米。有誰知道如何解決它的確切位置?任何幫助將不勝感激。在Google地圖上顯示正確的位置

這是我的代碼

public class GoogleMapsActivity extends MapActivity { 
public static final String TAG = "GoogleMapsActivity"; 
private MapView mapView; 
private LocationManager locationManager; 
Geocoder geocoder; 
Location location; 
LocationListener locationListener; 
CountDownTimer locationtimer; 
MapController mapController; 
MapOverlay mapOverlay = new MapOverlay(); 

@Override 
protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.googlemap); 
    initComponents(); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setSatellite(true); 
    mapController = mapView.getController(); 
    mapController.setZoom(16); 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if (locationManager == null) { 
     Toast.makeText(GoogleMapsActivity.this, 
       "Location Manager Not Available", Toast.LENGTH_SHORT) 
       .show(); 
     return; 
    } 
    location = locationManager 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (location == null) 
     location = locationManager 
       .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (location != null) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     Toast.makeText(GoogleMapsActivity.this, 
       "Location Are" + lat + ":" + lng, Toast.LENGTH_SHORT) 
       .show(); 
     GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
     mapController.animateTo(point, new Message()); 
     mapOverlay.setPointToDraw(point); 
     List<Overlay> listOfOverlays = mapView.getOverlays(); 
     listOfOverlays.clear(); 
     listOfOverlays.add(mapOverlay); 
    } 
    locationListener = new LocationListener() { 
     @Override 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     } 

     @Override 
     public void onProviderEnabled(String arg0) { 
     } 

     @Override 
     public void onProviderDisabled(String arg0) { 
     } 

     @Override 
     public void onLocationChanged(Location l) { 
      location = l; 
      locationManager.removeUpdates(this); 
      if (l.getLatitude() == 0 || l.getLongitude() == 0) { 
      } else { 
       double lat = l.getLatitude(); 
       double lng = l.getLongitude(); 
       Toast.makeText(GoogleMapsActivity.this, 
         "Location Are" + lat + ":" + lng, 
         Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }; 
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 1000, 10f, locationListener); 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener); 
    locationtimer = new CountDownTimer(30000, 5000) { 
     @Override 
     public void onTick(long millisUntilFinished) { 
      if (location != null) 
       locationtimer.cancel(); 
     } 

     @Override 
     public void onFinish() { 
      if (location == null) { 
      } 
     } 
    }; 
    locationtimer.start(); 
} 

public MapView getMapView() { 
    return this.mapView; 
} 

private void initComponents() { 
    mapView = (MapView) findViewById(R.id.googleMapview); 
} 

@Override 
protected boolean isRouteDisplayed() { 
    return false; 
} 

class MapOverlay extends Overlay { 
    private GeoPoint pointToDraw; 

    public void setPointToDraw(GeoPoint point) { 
     pointToDraw = point; 
    } 

    public GeoPoint getPointToDraw() { 
     return pointToDraw; 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
      long when) { 
     super.draw(canvas, mapView, shadow); 

     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(pointToDraw, screenPts); 

     Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
       R.drawable.pingreen); 
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); 
     return true; 
    } 
} 

}

+0

您使用的設備或模擬器的位置? – 2012-10-17 06:45:40

+0

感謝您的快速回復。我正在嘗試使用設備。 – bynu022

回答

0

最簡單的方法是使用MyLocationOverlay

https://developers.google.com/maps/documentation/android/reference/index?com/google/android/maps/MyLocationOverlay.html

創建並添加到地圖疊加,它會放置標記確切地說用戶在哪裏。

喜歡的東西

MyLocationOverlay myOvly = new MyLocationOverlay(this, mapView); 
    listOfOverlays.add(myOvly); 
    myOvly.enableMyLocation(); 

來放大

myOvly.runOnFirstFix(new Runnable() 
      { 
      public void run() 
      { 
       mapController.animateTo(myOvly.getLocation()); 
      } 
      }); 
+0

你能否提供一些示例代碼? – bynu022

相關問題