2

I am trying to geocode an place name, however when I run the function, I get,

"an empty string" in my console,

below is my code why would this be happening?

function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result = results[0].geometry.location;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    console.log(result);
}

UPDATE

Ok, so I have got my value going to the console, now when I try and push the value of element into the function, I get undefined as a return, below is the full code,

function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result = results[0].geometry.location;
            return result;
        } else {
            result = "Unable to find address: " + status;
            alert(result);
        }
    });
}

//getLatLong("YORK"); 

function loadScript(postcode){
    alert(postcode);
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize?"+postcode;
    document.body.appendChild(script);
   }

   /* load map on visitors location */
   function initialize(postcode){
     var myLatlng = new google.maps.LatLng(getLatLong(postcode));
     var myOptions = {
      center: myLatlng,
      zoom:13,
      disableDefaultUI: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
     }

     var map = new google.maps.Map(document.getElementById('map'), myOptions);
     google.maps.event.trigger(map, 'resize');
     map.setZoom(map.getZoom());
     var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map,
      icon:'http://google-maps-icons.googlecode.com/files/home.png',
     });
    }

And it is called like this,

 $(function(){
    $("dd a, dt a").live("click", function(){
        var self = $(this);
        $("#overlay").fadeIn('slow');
        var targetProcent = 85;
        var targetWidth = $(window).width() * (targetProcent / 100);
        var targetHeight = $(window).height() * (targetProcent / 100);   
        var targetX = ($(window).width() - targetWidth) / 2;
        var targetY = ($(window).height() - targetHeight) / 2 + $(document).scrollTop();
        $('#lightbox').height(700);
        $('#lightbox').width(targetWidth);
        $('#lightbox').load(self.attr("href"));
        loadScript($("#postcode").val());
        //usePointFromPostcode(document.getElementById('postcode').value, placeMarkerAtPoint)
        $('#lightbox').css({
            "position": "absolute", 
            "top": targetY+"px", 
            "left": targetX+"px"
        }).fadeIn('slow');
        return false;
    });
    });

$("#postcode").val() relates to an element that is loaded in when using load()

4

1 回答 1

4

您进行异步调用,然后打印到控制台。电话还没有回

解决方案:

将 console.log 移到回调中

编辑: 回调是您作为参数传递给地理编码的匿名函数,它的签名是function(results, status)您应该将console.log调用放在该函数的末尾

编辑您的编辑:

您的脚本网址看起来是错误的:

"http://maps.google.com/maps/api/js?sensor=false&callback=initialize?"+postcode

我很确定添加 ?postcode 不会做任何事情。如果有的话,网址应该只包含一个?在查询参数开始之前。似乎您想将邮政编码传递给initialize函数,最简单的方法是通过全局变量。

在函数中设置全局变量并在函数loadScript中读取initialize

于 2011-06-16T13:43:58.530 回答