未定义不是函数,Google地理位置 [英] Undefined is not a function, Google Geolocation

查看:114
本文介绍了未定义不是函数,Google地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <脚本

我试图在地图上显示地址作为标记。 type =text / javascriptsrc =http://maps.googleapis.com/maps/api/js?key=my_key&sensor=true&callback=initialize>< / script>
< script language =javascripttype =text / javascript>
navigator.geolocation.getCurrentPosition(foundLocation,noLocation);
var latitude;
var longitude;
var map;
函数foundLocation(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
}
函数noLocation(){
//在没有找到位置或用户拒绝访问的情况下在这里执行某些操作

函数initialize(){
var mapOptions = {
zoom:8,
center:new google.maps.LatLng(latitude,longitude),
mapTypeId:google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById(map_canvas),mapOptions);
// setMarkers();
}
函数addMarker(location){
var marker = new google.maps.Marker({
position:location,
map:map
} );
markersArray.push(marker);
}
< / script>


< span id =ctl00_ContentPlaceHolder1_lblGeneratedScript>< script language =javascripttype =text / javascript>
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':123 Test Dr.},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
addMarker(results [0] .geometry.location);
} else {
alert(Geocode failed!Reason:+ status);
}
});
< / script>
< / span>

但是,在这一行上

  var geocoder = new google.maps.Geocoder(); 

我得到未定义的类型错误Undefined is a function。。



在我选择的代码中,我省略了api键,并且改变了我用来测试的地址。 (我的家庭地址)

另外,我使用ASP.NET在span标记中生成脚本。

解决方案

在API加载完成之前,您无法构造Geocoder对象。在初始化方法中或之后调用它。


I'm trying to display an address as a marker on a google map element within my page.

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=my_key&sensor=true&callback=initialize"></script>
<script language="javascript" type="text/javascript">
    navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
    var latitude;
    var longitude;
    var map;
    function foundLocation(position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
    }
    function noLocation() {
        //Do something here in the case that no location is found, or user denies access
    }
    function initialize() {
        var mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(latitude, longitude),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        //setMarkers();
    }
    function addMarker(location) {
        var marker = new google.maps.Marker({
            position: location,
            map: map
        });
        markersArray.push(marker);
    }
</script>


<span id="ctl00_ContentPlaceHolder1_lblGeneratedScript"><script language="javascript"  type="text/javascript">
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': "123 Test Dr."}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            addMarker(results[0].geometry.location);
        } else {
            alert("Geocode failed! Reason: " + status);
        }
    });
</script>
</span>

But, on this line

var geocoder = new google.maps.Geocoder();

I am getting an uncaught type error of "Undefined is not a function."

I've omitted my api key in this selection of my code, as well as changed the address from what I am using to test. (my home address)

Also, I am using ASP.NET to generate the script within the span tags.

解决方案

You can't construct the Geocoder object before the API has finished loading. Call it in or after your initialize method.

这篇关于未定义不是函数,Google地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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