Google 地图 v3 地理编码

发布于 2024-11-15 21:19:04 字数 3024 浏览 3 评论 0原文

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

在控制台中得到“空字符串”,

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

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

更新

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

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() 时加载的元素相关

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2024-11-22 21:19:04

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

解决方案:

将 console.log 移至回调

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

编辑您的编辑:

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

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

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

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

you make an async call, and then print to console. the call hasnt returned yet

solution:

move the console.log into the callback

edit:
the callback is the anonymous function that you pass as an argument to geocode, its signature is function(results, status) you should put your console.log call at the END of that function

EDIT for your edit:

your script url looks like its wrong:

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

i am pretty sure that tacking on ?postcode doesnt do anything. If anything, the url should only contain one ? before the start of the query params.
It seems like you want to pass the postcode to the initialize function, the easiest way to do that would be via a global variable.

set the global variable in the loadScript function and read it in the initialize function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文