2016-03-15 102 views
3

JSON響應:從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地圖上。但我無法放置標記。

回答

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


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

u能告訴我如何添加地名和自定義標記 – Risk

+0

markerOptions.position(點).title僞(「地名」); – PpL

+0

爲地址名稱,地址爲 markerOptions.position(point).title(「地點名稱」).snippet(「Full Address」); – PpL

0

後,如果我是正確的,你的評論代碼必須像下面一樣正常工作。

對於你每次循環,

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

這是你如何爲標記添加標題和自定義圖標(fromResource/fomFile/...),你可以使用甚至拼合圖像.flat(true)