Skip to content Skip to sidebar Skip to footer

I Didn't Get My Latitude And Longitude In My Mapview?

Here I wrote code for getting lat and long values in my mapview activity, but its getting forced closed is there any modification or other methods to get these? public class Gpsove

Solution 1:

I don't know what exactly you are trying to do here, but I can see some errors:

  • class declaration is wrong (you have to use MapActivity not Activity):

public class GpsoverlayActivity extends MapActivity

  • about gp, you didn't put a value in it, insert any latitude and longitude like this:

    GeoPoint gp= newGeoPoint((int)(26.2*1.0E6), (int)(52.6*1.0E6));
    

if you are trying to marker on the map I suggest you create another class extends ItemizedOverlay like this structure:

publicclassMapViewItemizedOverlayextendsItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = newArrayList<OverlayItem>();
    private Context mContext;

    publicMapViewItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
         mContext = context;
    }

    @Overrideprotected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }


    @Overridepublicintsize() {
        // TODO Auto-generated method stubreturn mOverlays.size();
    }

    publicvoidaddOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }


    @OverridepublicbooleanonTap(int index)
    {   
        //get the marker on the map from hereOverlayItemitem= mOverlays.get(index);
        OverlayItem item=getItem(i);
        GeoPoint geo=item.getPoint();
        Point pt=map.getProjection().toPixels(geo, null);
        String message=String.format("Lat: %f | Lon: %f\nX: %d | Y %d",
        geo.getLatitudeE6()/1000000.0,
        geo.getLongitudeE6()/1000000.0,
        pt.x, pt.y);
        Toast.makeText(getApplicationContext(),
        message,
        Toast.LENGTH_LONG).show();
    returntrue;
    }

and use it like this in the MapActivity:

MapViewmapView= (MapView) findViewById(R.id.mapview);
MapControllermc= mapView.getController();
List<Overlay> mapOverlays = mapView.getOverlays();
//add any icon here for the markerDrawabledrawable= MainActivity.this.getResources().getDrawable(R.drawable.icon);
MapViewItemizedOverlayitemizedOverlay=newMapViewItemizedOverlay(drawable,this);
//insert any lat and lng as integers hereGeoPointpoint1=newGeoPoint(lat,lng);
OverlayItemoverlayitem1=newOverlayItem(point1, "Info", "You are here!" );
itemizedOverlay.addOverlay(overlayitem1);
mapOverlays.add(itemizedOverlay);
mc.animateTo(point1);

Post a Comment for "I Didn't Get My Latitude And Longitude In My Mapview?"