jQuery and Google Maps json response

后端 未结 4 2101
庸人自扰
庸人自扰 2021-01-12 21:33

I am having troubles getting geo location info from google maps api

the code is pretty straight forward

$.ajax({
    type: \"GET\",
    cache: false,         


        
4条回答
  •  天命终不由人
    2021-01-12 22:12

    if it helps somebody... I gave up and I completely change the logic itself and now it works as expected:

    first I appended google maps js into the body

    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=get_longlat';
    $("body").append( script );
    

    as you can see I specified the callback function get_longlat...

    so I defined this function and used google's geocode object

    function get_longlat(address){
    
        var geocoder = new google.maps.Geocoder();
    
        if(!address){
            var p = US.get("location_postal");
            var c = US.get("location_city");
            var a = US.get("location_address");
            var address = a + ', ' + p + ' ' + c + ', Slovenia';
        }
    
        if (geocoder) {
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    US.set("location_lat", results[0].geometry.location.lat(), 60);
                    US.set("location_lon", results[0].geometry.location.lng(), 60);
    
                    $('#location_setter').dialog('close');
                } 
                else {
                    console.log('No results found: ' + status);
                }
            });
        }
    }
    

    The US variables inside is our own namespace for USER SETTINGS

    hope it helps somebody

提交回复
热议问题