我现在正在推进与Google地图服务相关的项目。我想在我的地图中指出我当前的位置。为此,我有一个使用itemizedoverlay的想法。我在Geopoint的帮助下检索当前位置。但是当我在我的代码中使用itemizedoverlay时,无法获取位置,我在OnCreate方法中使用它。如果我将其移动到OnLocationChanged方法,则itemizedoverlay将不会选择GeoPoint(point)并以错误结束,并且在当前条件下,应用程序将被压缩。
MapView mapView;
Location location;
LocationManager locationmanager;
LocationListener locationlistener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
//locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locationlistener = new GPSLocationListener();
// locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER 0,0,locationlistener);
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
OverlayItem overlayitem = new OverlayItem(point,"","" );
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
/* private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
OverlayItem overlayitem = new OverlayItem(point,"","" );
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
} */
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Disabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
你能为我建议一个解决方案吗?在此先感谢,如果我在这里犯了任何错误,我很抱歉。
答案 0 :(得分:2)
试试这个 -
/**
* Setting Google Map to provided location
*/
private void setMaptoLocation(Location location) {
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
int LAT = (int)(location.getLatitude() * 1E6) ;
int LNG = (int)(location.getLongitude() * 1E6) ;
GeoPoint point = new GeoPoint(LAT, LNG);
mapController = mapView.getController();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
OverlayItem overlayitem = new OverlayItem(point,"","" );
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapController.setZoom(ZOOM_LEVEL - ZOOM_LEVEL / 2); // Set zoom level per requierment
mapController.animateTo(point);
}
答案 1 :(得分:2)
您可以使用com.google.android.maps.MyLocationOverlay,它会自动在地图视图中显示您的位置。它甚至会在旅行时更新您的位置。
注意:分别在onResume和onPause上显示调用enableMyLocation和disableMyLocation以获得更好的性能。
@Override
protected void onResume() {
super.onResume();
if (locationOverlay != null)
locationOverlay.enableMyLocation();
}
@Override
protected void onPause() {
if (locationOverlay != null)
locationOverlay.disableMyLocation();
super.onPause();
}
<强> EDITED 强>
MyLocationOverlay locationOverlay;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
locationOverlay = new MyLocationOverlay(this, mapView);
List<Overlay> mapOverlays = mapView.getOverlays();
mapOverlays.add(locationOverlay);
}
还添加onResume()和onPause()方法。
答案 2 :(得分:0)
我认为你必须使用Google Maps Android API v2
有关详细信息,请查看Google Maps Android API v2。
请检查上述链接中示例代码的链接。 sample code
答案 3 :(得分:0)
位置是没有的,所以我猜它是空的。还有一件事,如果你有Lat和Long,逐项列出肯定会在地图上显示位置。 在Manifest中检查权限 Access_Course_location 和 Access_fine_location 。