如何从接收到的JSON中单独获得最新的lng值

how to get lat lng value separately from received JSON?

本文关键字:单独获 最新 lng JSON      更新时间:2023-09-26

我在像'obj'这样的var中从谷歌地图api获得以下JSON响应。当我提醒obj.name时,我看到的是"岩石上的煎饼"。如果我需要图标,我这样写- obj。图标。但是我怎么能得到单独的价值lat/lng。当我警告obj.geometry.location时,我得到了-(-33.87054,151.198815),但我需要它们分开,而不是像这样。任何建议。谢谢所有。

{
"html_attributions" : [],
"results" : [
  {
     "geometry" : {
        "location" : {
           "lat" : -33.87054,
           "lng" : 151.198815
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
     "id" : "c71365287e7606bd21a3311d21fda087830b7813",
     "name" : "Pancakes on the Rocks",
     "opening_hours" : {
        "open_now" : true
     },
     "photos" : [
      ...

function createMarkers(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
    createResultMarker(results[i]);
    }
  } 
}

function createResultMarker(obj) {
  alert('title: ' + obj.name);  // Alert correct name.
  alert(obj.geometry.location);
  alert(obj.geometry.location.lat);
  distance = getDistanceFromLatLonInKm(init_lat, init_lng, obj.geometry.location.lat, obj.geometry.location.lng);
}

使用obj.geometry.location.lat()obj.geometry.lng(),它们是函数。

您实际上没有使用您发布的JSON,您使用的是google.maps.places服务,该服务返回google.maps.LatLng对象