试图通过google api v3获取地址的位置 [英] trying to get location from address using google api v3

查看:80
本文介绍了试图通过google api v3获取地址的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html中有这个javascript,我想在这里做的是获得给定位置的地图表示形式。

 < ; script type =text / javascript> 
var geocoder;
var map;
函数getmaploc(){
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom:8,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
geocoder.geocode('cairo',function(results,status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results [0]。 geometry.location);
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
} else {
alert(由于以下原因,Geocode不成功:+ status);
}
map = new google.maps.Map(document.getElementById( map_canvas),myOptions);
});
}
< / script>

< body onload =getmaploc()>
< div id =map_canvasstyle =width:320px; height:480px;>< / div>
< / body>

我无法弄清楚这有什么问题吗?

解决方案

有多个错误,


  • 省略的大括号

  • 首先初始化映射

  • geocode()需要一个GeocoderRequest对象作为参数





    固定代码:

      var geocoder; 
    var map;
    函数getmaploc(){
    geocoder = new google.maps.Geocoder();
    var myOptions = {
    zoom:8,
    mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById(map_canvas),myOptions);

    geocoder.geocode({
    地址:'cairo'
    },函数(结果,状态){
    console.log(结果);
    if(status == google.maps.GeocoderStatus.OK){
    map.setCenter(results [0] .geometry.location);
    new google.maps.Marker({
    map:地图,
    位置:results [0] .geometry.location
    });
    } else {
    alert(地理编码失败,原因如下:+ status) ;
    }

    });
    }


    I have this javascript in my html what i want to do here is to get a map representation given the location

    <script type="text/javascript">
    var geocoder;
    var map;
    function getmaploc() {
        geocoder = new google.maps.Geocoder();
        var myOptions = {
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        geocoder.geocode( 'cairo', function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        });
    }
    </script>
    
    <body onload="getmaploc()">
      <div id="map_canvas"  style="width: 320px; height: 480px;"></div>
    </body> 
    

    I cant figure out whats wrong with this any help??

    解决方案

    There are multiple errors,

    • a omitted brace
    • initialize the map first
    • geocode() expects a GeocoderRequest-object as argument

    fixed code:

    var geocoder;
    var map;
    function getmaploc() {
        geocoder = new google.maps.Geocoder();
        var myOptions = {
            zoom : 8,
            mapTypeId : google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
        geocoder.geocode({
            address : 'cairo'
        }, function(results, status) {
            console.log(results);
            if(status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                new google.maps.Marker({
                    map : map,
                    position : results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
    
        });
    }
    

    这篇关于试图通过google api v3获取地址的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆