Question

How to show latitude and longitude which I received from server on android map. I implemented google map V2 but don't know how to show gps coordinates on map. I came here after a lot of research and wasn't able to find a single reasonable solution for it. Can anyone please help me out? So far my code of latitude and longitude values which I am reviving from server.

                String lat, lon;
                double l1,l2;

                DB db = new DB(getApplicationContext());

                HashMap<String,String> gps = new HashMap<String, String>();
                gps = db.getGps();

                lat = user.get("lati");
                lon = user.get("longi");

Okay when I try to add marker on map according to latitude and longitude from server my app got force close and gives NullPointerException.

        free.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            String lat, lon;
            double l1,l2;

            DB db = new DB(getApplicationContext());

            HashMap<String,String> gps = new HashMap<String, String>();
            gps = db.getGps();

            lat = user.get("lati");
            lon = user.get("longi");

            l1 = Double.parseDouble(lat);
            l2 = Double.parseDouble(lon);
            map.addMarker(new MarkerOptions().position(new LatLng(l1, l2))
                     .title("My Map Title"));                   
Was it helpful?

Solution

Your "map" variable may not be initialized. I add a marker like this:

SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = smf.getMap();
if ( mMap != null ) {
    LatLng newLatLong = new LatLng(dblLat, dblLong);
    mMap.addMarker(new MarkerOptions().position(newLatLong)
                  .title(name)
                  .snippet(fullStrAddr));
}

OTHER TIPS

Add a marker like this

Marker mymarker= map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
      .title("My Map Title"));

This will set a marker at the latitude and longitude which was sent from the server..

Try it..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top