簡體   English   中英

解析從Google Maps API返回的JSON

[英]parsing JSON returned from Google Maps API

我正在嘗試對地址進行地理編碼,在本例中為Google的總部。

當我嘗試發出jQuery $.ajax()請求以獲取JSON時,它說出Uncaught SyntaxError:Unexpected token:

我似乎無法弄清楚意外的位置在哪里。 我想做的是將一個地址中的經緯度解碼為字符串,然后使用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);
});

三個問題:

  1. 告訴$.ajax()在調用JSON返回API時期望jsonp
  2. var place = mapResultsi]; 應該是var place = mapResults[i];
  3. location: new google.maps.LatLng(queryLat, queryLong)后缺少逗號location: new google.maps.LatLng(queryLat, 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]);
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM