从JSON响应在Google Map上添加多个标记

时间:2016-03-15 06:26:20

标签: android json google-maps google-maps-markers

Json的回应:

{"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"}]}

我做了什么:

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

我得到的纬度和经度:

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

我想要的是在我的JSON响应中将多个带地名的标记放在Google Map上。但是我无法放置标记。

3 个答案:

答案 0 :(得分:2)

尝试以下代码行: -

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


  }

答案 1 :(得分:0)

可能会有所帮助, 使用方法,

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

为了调用这些方法,在从json获取每个

的纬度和经度之后
drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng))); 

答案 2 :(得分:0)

如果我是对的,您的评论代码必须正常工作,如下所示。

对于每次循环迭代,

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

这是你可以为标记添加标题和自定义图标(fromResource / fomFile / ...)的方法,甚至可以使用.flat(true)

来展平图像