解析从Google Maps API返回的JSON
问题描述:
我试图对地址进行地址解析,在这种情况下,Google地址是HQ。解析从Google Maps API返回的JSON
当我试图让jQuery的$.ajax()
请求得到JSON,它说:未捕获的SyntaxError:意外的标记:我好像
无法揣摩出意外:是。我想要做的是将地址作为一个字符串解码lat和long,并使用Places API找到这些坐标附近的机制。
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=myKey',
dataType: 'jsonp',
jsonp: 'callback',
method: 'GET',
success: function(results){
console.log(results);
queryLat = results['results']['geometry']['location']['lat'];
queryLong = results['results']['geometry']['location']['lng'];
console.log('latitude: '+queryLat);
console.log('longitude: '+queryLong);
function callback(mapsResults, status){
if(status == google.maps.places.PlacesServiceStatus.OK){
for(var i = 0; i < mapResults.length; i++){
var place = mapResultsi];
createMarker(mapResults[i]);
}
}
}
var map;
var service;
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(queryLat, queryLong),
zoom: 15
});
var request ={
location: new google.maps.LatLng(queryLat, queryLong)
radius:'500',
query:'mechanic'
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
});
答
三个问题:
- 告诉
$.ajax()
期待jsonp
当你location: new google.maps.LatLng(queryLat, queryLong)
后调用的JSON API返回
-
var place = mapResultsi];
应该var place = mapResults[i];
- 缺少一个逗号
答
function callback(mapsResults, status){
if(status == google.maps.places.PlacesServiceStatus.OK){
for(var i = 0; i < mapResults.length; i++){
**var place = mapResultsi];**
createMarker(mapResults[i]);
}
}
}
'var place = mapResultsi];'应该是'var place = mapResults [i];' – 2014-10-01 21:08:24
''location:new google.maps.LatLng(queryLat,queryLong)'' – 2014-10-01 21:13:46
@PaulRoub谢谢。 – wordSmith 2014-10-01 21:15:12