1

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"));                   
Gravitoid
  • 1,294
  • 1
  • 20
  • 20
user3593284
  • 89
  • 1
  • 7
  • This will probably help: http://stackoverflow.com/questions/23112784/how-to-get-gps-coordinates-from-all-the-users-of-my-application/ – nKn May 01 '14 at 15:55

2 Answers2

1

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));
}
Gravitoid
  • 1,294
  • 1
  • 20
  • 20
0

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..

Lal
  • 14,726
  • 4
  • 45
  • 70