从外部api网址添加标记到谷歌地图?

时间:2016-11-09 17:37:17

标签: jquery json google-maps google-maps-api-3

我需要在api的网址中添加Google地图中的所有标记,例如http://192.168.99.100:8080/api/v1/location此网址显示:

[{"nid":"23","title":"Acopio en xalapa","created":"1478207996","modified":"1478207996","body":"Acopio en xalapa","cover":"http:\/\/192.168.99.100:8080\/sites\/default\/files\/styles\/original\/public?itok=GVqc4SZo","category":{"title":"Centro de acopio","detail":""},"address":"Venustiano carranza xalapa, veracruz","lat":"-123.1465","lng":"49.3026"},{"nid":"19","title":"Sucursal 1","created":"1476996732","modified":"1477666258","body":"esta es una sucursal muy bonita","cover":"http:\/\/192.168.99.100:8080\/sites\/default\/files\/styles\/original\/public?itok=GVqc4SZo","category":{"title":"Sucursal","detail":""},"address":"Av tripulacion #34, col. nolose","lat":"-123.146567","lng":"49.302600"}]

我必须根据该网址生成的json的纬度(lat)和长度(lng)显示地图中的所有标记。

谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

似乎您的内容是一个对象数组,然后假设您的结果网址是在名为my_var的var中分配的,您可以通过这种方式访问​​

 my_var[0].nid  // 23
 my_var[0].title// Acopio en xalapa
 .....
 my_var[0].address // Venustiano carranza xalapa, veracruz
 my_var[0].lat  // -123.1465
 my_var[0].lng  // 49.3026

 my_var[1].nid  // 19
 my_var[1].title// Sucursal 1
 .....
 my_var[1].address // Av tripulacion #34, col. nolose
 my_var[1].lat  // -123.146567
 my_var[1].lng  //49.302600

假设您可以创建地图,则可以使用此标记填充地图

var markers = [];

for (i=0; i<my_var.length; i++){
  markers[i] = new google.maps.Marker({
    position: new google.maps.LatLng( my_var[i].lat, my_var[i].lng),
    map: image
  });