MyLocationOverlay:getMyLocation返回null

时间:2010-07-15 08:48:10

标签: android google-maps location

我有一个mapview页面,可以显示地图中的当前位置。

我正在使用MyLocationOverlay。代码如下:

myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();

myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
 geopoint = myLocationOverlay.getMyLocation();
 ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable);
 try{
  overlayitem = new OverlayItem(geopoint, "", "");
 }
 catch(Exception e){
  e.printStackTrace();
 }
 itemizedoverlay.addOverlay(overlayitem);
 mapOverlays.add(itemizedoverlay);
 mapController.animateTo(geoPoint);

 }
});

以上代码通过gps工作并获取当前位置,并在该点设置标记。

但是try块中的代码是个问题。 我在geopoint内获得空值。

实际上我必须得到这一点,并用距离和所有进行一些计算。为此,我必须在该geopoint内获得正确的值。

有人可以告诉解决方案吗?

4 个答案:

答案 0 :(得分:1)

我一直在研究同样的问题一天,这就是我发现的。出于某种原因,这是有效的

LocationManager lm;
GeoPoint gp;        

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (lastKnownLoc != null){
  int longTemp = (int)(lastKnownLoc.getLongitude()* 1000000);
  int latTemp = (int)(lastKnownLoc.getLatitude() * 1000000);
  gp =  new GeoPoint(latTemp, longTemp);
}

这不是

gp = myLocOverlay.getMyLocation();

答案 1 :(得分:0)

可以使用MyLocationOverlay类和LocationManager来跟踪当前位置。使用MyLocationOverlay,下面的代码可以工作..

MyLocationOverlay myLocationOverlay;
MapView mapView;
MapController mapcontroller;
OverlayItem overlayitem;
List<Overlay> mapoverlays;
GeoPoint geopoint;

protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.currentlocation);

  mapView = (MapView) findViewById(R.id.map);

  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(false);
  mapView.setStreetView(true);
  mapView.setTraffic(true);

  mapcontroller = mapView.getController();
  mapoverlays = mapView.getOverlays();
  drawable = this.getResources().getDrawable(R.drawable.ic_map_red);
  mapView.invalidate();

  getCurrentLocation();             
}

public void getCurrentLocation(){  

   myLocationOverlay = new MyLocationOverlay(this, mapView);
   myLocationOverlay.enableCompass();
   myLocationOverlay.enableMyLocation();
   myLocationOverlay.runOnFirstFix(new Runnable() {

     public void run() {

   ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable);

   try {
    latitude = myLocationOverlay.getMyLocation().getLatitudeE6();
    longitude = myLocationOverlay.getMyLocation().getLongitudeE6();
            geopoint = new GeoPoint((int) (latitude), (int) (longitude));
    overlayitem = new OverlayItem(geopoint, "", "");
    itemizedoverlay.addOverlay(overlayitem);
    mapoverlays.add(itemizedoverlay);
    mapcontroller.animateTo(geopoint);
    mapcontroller.setZoom(16);
   } 
   catch (Exception e) {}
    }           
  });       
}

答案 2 :(得分:0)

如果覆盖:

onLocationChanged();

请记得致电:

super.onLocationChanged();

在您自己的代码之后或之前。这似乎可以确保变量

getMyLocation()

返回不为空。

答案 3 :(得分:0)

楼上的朋友让我上升,尝试覆盖任何东西,既不画也不画drawMyLocation。 就像这样:

MyLocationOverlay myLocationOverlay = new SelfLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.disableCompass();
mapView.getOverlays().add(myLocationOverlay);