javascript - 如何从 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,|¢er=38.892062,-77.019912&zoom=12&rand=306744981"
}
]
}
]
}
这不起作用,我的页面中没有地图.. 结果必须有,生成地图的谷歌地图,中心和地址上的标记,我放在 mapQuest 给我的 url 上......
解决方案
JsonMapQuest
显然$jsonMapQuest
不是一回事......
您必须决定如何调用它,然后坚持使用它,例如:
var JsonMapQuest = ...
var lat = JsonMapQuest.results.locations.latLng.lat;
var lng = JsonMapQuest.results.locations.latLng.lng;
可能这也可以工作,少了几行,因为它跳过了所有无用的处理:
var position = JsonMapQuest.results.locations.latLng;
推荐阅读
- reactjs - Is it posible to sync latest state in function
- javascript - How to add days in the current date? using script and django
- mysql - python3 virtualenv(Mac OS)中的mysqlclient安装失败
- node.js - How can I use two "show" methods in the controller?
- c# - No node_modules folder after npm install inside Docker
- javascript - 错误:未找到与请求 URI 匹配的 HTTP 资源 - 使用对象类型传递值
- reactjs - 从非组件访问 next-redux-wrapper 的存储
- php - PHP中的TCP套接字
- javascript - 如何连接通过 Node.js 中的 Mongoose 通过两个回调调用创建的字符串?
- string - 使用替换列表及其位置替换文件中的字符串