首页 > 解决方案 > 如何从 javascript whit mapQuest 中的链式 json 中获取特定值

问题描述

我 ppl,我试图从 MapQuest Decoding Json 中获取 lat 和 lng ......但我的 javascript 太糟糕了,我不知道我是否应该使用 jquery,也许我可以简单地解析一下......

我向 Json 发送地址以计算 Lat 和 Lng ......

<input type="hidden" id="address" value=" @(String.Format("{0} - {1} - {2} - {3}",
                                                              this.Model.Calle, this.Model.Altura,
                                                              this.Model.localidadesInmueble.nombre,
                                                              this.Model.provinciasInmueble.nombre)) " />
                <output id='map' style="width:700px; height:525px; margin: 0; padding: 0;" />

这里是我的 Javascript 代码:

<script>
    // Initialize and add the map
    function initMap() {

        var JsonMapQuest = JSON.parse('http://open.mapquestapi.com/geocoding/v1/address?key=MYKEY&location=Address');
        //var JsonMapQuestDecoded = decodeURI($JsonMapQuest);
        
        var lat1 = $jsonMapQuest.results.locations.latLng.lat;
        var lon1 = $jsonMapQuest.results.locations.latLng.lng;
        // Posicion del mapa
        var pos = { lat: lat1, lng: lon1 };
        // El mapa, centrado en la posicion
        var map = new google.maps.Map(
            document.getElementById('map'), { zoom: 8, center: pos });
        // El marcador, posicionado en la posicion
        var marker = new google.maps.Marker({ position: pos, map: map });
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"
        async defer></script>

这是我从 url 得到的 JSON ......

{
  "info": {
    "statuscode": 0,
    "copyright": {
      "text": "© 2018 MapQuest, Inc.",
      "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
      "imageAltText": "© 2018 MapQuest, Inc."
    },
    "messages": []
  },
  "options": {
    "maxResults": -1,
    "thumbMaps": true,
    "ignoreLatLngInput": false
  },
  "results": [
    {
      "providedLocation": {
        "location": "Washington,DC"
      },
      "locations": [
        {
          "street": "",
          "adminArea6": "",
          "adminArea6Type": "Neighborhood",
          "adminArea5": "Washington",
          "adminArea5Type": "City",
          "adminArea4": "District of Columbia",
          "adminArea4Type": "County",
          "adminArea3": "DC",
          "adminArea3Type": "State",
          "adminArea1": "US",
          "adminArea1Type": "Country",
          "postalCode": "",
          "geocodeQualityCode": "A5XAX",
          "geocodeQuality": "CITY",
          "dragPoint": false,
          "sideOfStreet": "N",
          "linkId": "282772166",
          "unknownInput": "",
          "type": "s",
          "latLng": {
            "lat": 38.892062,
            "lng": -77.019912
          },
          "displayLatLng": {
            "lat": 38.892062,
            "lng": -77.019912
          },
          "mapUrl": "http://open.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-1,38.892062,-77.019912,0,0,|&center=38.892062,-77.019912&zoom=12&rand=306744981"
        }
      ]
    }
  ]
}

这不起作用,我的页面中没有地图.. 结果必须有,生成地图的谷歌地图,中心和地址上的标记,我放在 mapQuest 给我的 url 上......

标签: javascriptjqueryhtmljsonmapquest

解决方案


JsonMapQuest显然$jsonMapQuest不是一回事......

您必须决定如何调用它,然后坚持使用它,例如:

var JsonMapQuest = ...
var lat = JsonMapQuest.results.locations.latLng.lat;
var lng = JsonMapQuest.results.locations.latLng.lng;

可能这也可以工作,少了几行,因为它跳过了所有无用的处理:

var position = JsonMapQuest.results.locations.latLng;

推荐阅读