google geocode vai ajax with jsonp, giving an error

人盡茶涼 提交于 2019-12-13 05:20:00

问题


i am trying to use google geocode vai ajax with jsonp, here is the code

    jQuery("#getaddress").on("click", function () {
        jQuery.ajax({
            dataType: 'jsonp',
            url: 'http://maps.googleapis.com/maps/api/geocode/json',
            data: {
                address: 'rajshahi',
                sensor: false
            },
            success:function(results){
            console.log(results);
        }
    });

});

josn return is cominf nicely but it's giving this error

invalid label
    [Break On This Error]   

    "results" : [

    json?c...6919370 (line 2, col 3)

Please let me know what need to do to solve this.


回答1:


I'm not familiar with the Google Geocoder API, but it looks like it does not support JSONP requests, i.e. is returning plain JSON and not wrapping it in a javascript function. You'll need to use the google.maps.geocoder as described in the documentation




回答2:


This might be helpful to you....

var district = $('#district').val();
        var vdc = $('#vdc').val();
        var location = $('#locate').val();


        var myAddressQuery = location +","+ vdc+","+district ;
        var geocoder = new google.maps.Geocoder(); 
        geocoder.geocode(
            { address : myAddressQuery, 
              region: 'no' 
            }, function(results, status){
                  if(status==google.maps.GeocoderStatus.OK){
                    var lat= results[0].geometry.location['Ya'];
                    var lng = results[0].geometry.location['Za']
                    position = new OpenLayers.LonLat(lng,lat).transform( fromProjection, toProjection);
                    data.openlayers.setCenter(position,zoom);  
                 }

        }); 

for more you can visit https://developers.google.com/maps/documentation/javascript/geocoding?hl=no-NO



来源:https://stackoverflow.com/questions/9875445/google-geocode-vai-ajax-with-jsonp-giving-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!