views:

46

answers:

2

I have a mapview page that displays the current location in the map.

I am using MyLocationOverlay for the purpose. The code for that goes as follows:

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);

 }
});

The above code works and gets the current location through gps and sets a marker at that point.

But the code inside try block is the issue. I am getting null value inside geopoint.

Actually I have to get that point and do some calculations with distance and all. For that I have to get the correct value inside that geopoint.

Can anyone please tell the solution for this?

A: 

I've been working on the same problem for a day now and here's what I've discovered. For some reason this works

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);
}

and this doesn't

gp = myLocOverlay.getMyLocation();
Janne Oksanen
A: 

Hi,

Tracking current location can be done by using MyLocationOverlay class and through LocationManager. Using MyLocationOverlay, the code below works..

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) {}
    }           
  });       
}
Anju