gpt4 book ai didi

android - 当前位置未在 android 中拾取

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:08 25 4
gpt4 key购买 nike

我现在正着手一个与 Google map 服务相关的项目。我想在我的 map 上指出我当前的位置。为此,我想到了使用 itemizedoverlay。我在 Geopoint 的帮助下检索了当前位置。但是当我在我的代码中使用 itemizedoverlay 时,无法获取位置,我在我的 OnCreate 方法中使用它。如果我将其移动到 OnLocationChanged 方法,则 GeoPoint(point) 将不会被 itemizedoverlay 拾取并将以错误结束,并且在当前情况下,应用程序会崩溃。

        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

}

你能给我一个解决方案吗??提前致谢,如果我在这里犯了任何错误,我深表歉意。

最佳答案

您可以使用 com.google.android.maps.MyLocationOverlay,它会自动在您的 map View 中显示您的位置。它甚至会在旅行时更新您的位置。

注意:您分别在 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();
}

已编辑

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() 方法。

关于android - 当前位置未在 android 中拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14438349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com