繁体   English   中英

Google Maps v3地理编码

[英]Google maps v3 geocode

我正在尝试对地名进行地理编码,但是当我运行该功能时,

我的控制台中的“空字符串”

下面是我的代码为什么会发生这种情况?

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

好的,所以我已经获得了输入控制台的值,现在,当我尝试将element的值推入函数时,我得到的返回值未定义,下面是完整的代码,

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',
     });
    }

像这样,

 $(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()与使用load()时加载的元素相关

您进行异步调用,然后打印到控制台。 通话尚未返回

解:

将console.log移到回调中

编辑:回调是您作为地址解析参数传递的匿名函数,其签名是function(results, status) ,应将console.log调用放在该函数的结尾

编辑您的编辑内容:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM