Parsing Google Geo API (Reverse Geocoding) with jQuery

前端 未结 4 1697
闹比i
闹比i 2020-12-21 11:43

I\'m sure I\'m not the only one, but I cannot parse/return anything when jQuery takes a request to lookup an address when the data being requested is the latitude

相关标签:
4条回答
  • 2020-12-21 11:55

    As noted above by Bjorn there are security constraints due to the endpoint being on a different domain. However you do NOT need to use geocoder, as all that does is make an ajax request - it does little more than what you are doing now.

    As Shidhin Cr notes above, one way around the security problem is that you can append callback=?, but all that really is doing is performing a task that jQuery can do for you automatically by using the "jsonp" dataType argument.

    Also if you use $.getJSON it should automatically note that this is on a remote server and upgrade the request to jsonp automatically for you - but I might be remembering that wrong, there were some arguments about the jquery documentation related to that particular call.

    In either case both of the above suggestions are correct, they were just lacking detail.

    0 讨论(0)
  • 2020-12-21 12:10

    try use $.getJSON to get the result

    Example

    var latlng = "40.06125658,-7.745361328";
    var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latlng + "&sensor=false";
    $.getJSON(url, function (data) {
        for(var i=0;i<data.results.length;i++) {
            var adress = data.results[i].formatted_address;
            alert(adress);
         }
    })
    
    0 讨论(0)
  • 2020-12-21 12:11

    Change the data parameter like this . you can see the response coming properly

    data: 'latlng=' + geolocation + '&sensor=false&callback=?'
    
    0 讨论(0)
  • 2020-12-21 12:15

    You need to use googles geocoder for security reasons. See answer here.

    0 讨论(0)
提交回复
热议问题