未定义不是一个函数,谷歌地理定位

Undefined is not a function, Google Geolocation

本文关键字:函数 谷歌 定位 一个 未定义      更新时间:2023-09-26

我正试图在页面中的谷歌地图元素上显示一个地址作为标记。

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

但是,在这条线上

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

我得到一个未捕获类型的错误"未定义不是一个函数。"

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

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

在API完成加载之前,不能构造Geocoder对象。在initialize方法中或之后调用它。