Add multiple markers on Google Map from JSON respo

2019-05-09 08:16发布

Json response:

{"status":true,"message":"Successfully Alert Message","Data":[{"longitude":"70.7652417","latitude":"22.2889975","alert_message":"abcd"},{"longitude":"70.7652417","latitude":"22.2888975","alert_message":"pqr",},{longitude":"70.7662417","latitude":"22.2898975","alert_message":"xyz"}]}

What I have done:

arraylist = parseContent.getUsers(response);
adapter = new ListViewAdapter(getApplicationContext(), arraylist);
Log.d("typelist", arraylist.toString());
lv.setAdapter(adapter);

for (int i =0;i<arraylist.size();i++)
{
    longitude1 = arraylist.get(i).get("longitude");
    latitude1 = arraylist.get(i).get("latitude");

    // MarkerOptions mp = new MarkerOptions();

    Double long1= Double.parseDouble(longitude1);
    Double lat1= Double.parseDouble(latitude1);

    /* mp.position(new LatLng(long1,lat1));
    mp.title("my position");
    googleMap.addMarker(mp);*/
    /*  Marker marker =  googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(long1, lat1))
        .title("Hello world")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag)));*/
    // marker.add(marker);
    // arraylist.add(marker);
    // googleMap.addMarker(marker);

    googleMap.addMarker(new MarkerOptions().position(new LatLng(long1, lat1)));
    Log.e("PlaceLL",lat1+" "+long1);
}

Latitude and longitude I am getting:

LL: 22.2889975 70.7652417
LL: 22.2888975 70.7652417
LL: 22.2898975 70.7662417

What I want is to put multiple markers with place name on Google Map, from my JSON response. But I am not able to place markers.

3条回答
Bombasti
2楼-- · 2019-05-09 08:38

If I am right, your commented code must work fine like below.

For every loop iteration you have,

map.addMarker(new MarkerOptions()
    .position(new LatLng(lat, long))
    .title(title))
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));

This is how you can add title and custom icon (fromResource/ fomFile/ ...) for marker, you can even flatten image using .flat(true)

查看更多
爷、活的狠高调
3楼-- · 2019-05-09 08:41

Try these lines of code for it:-

 private Marker locationMarker;

 for (int i = 0; i < arraylist.size(); i++) 
 {
   if(i==0) ////FOR ANIMATING THE CAMERA FOCUS FIRST TIME ON THE GOOGLE MAP
   {
    CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(Double.parseDouble(arraylist.get(i).getLatitute()), Double.parseDouble(arraylist.get(i).getLongitute()))).zoom(15).build();
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
   }

   locationMarker = googleMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(arraylist.get(i).getLatitute()), Double.parseDouble(arraylist.get(i).getLongitute()))).icon(BitmapDescriptorFactory.fromResource(R.drawable.YOUR_DRAWABLE)));


  }
查看更多
神经病院院长
4楼-- · 2019-05-09 08:41

May be these will help, Use method,

private void drawMarker(LatLng point){
    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions = new MarkerOptions();

    // Setting latitude and longitude for the marker
    markerOptions.position(point);

    // Adding marker on the Google Map
    googleMap.addMarker(markerOptions);
}

And for calling these method, after getting latitude and longitude from json for each

drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng))); 
查看更多
登录 后发表回答