未捕获(在承诺中)TypeError:无法读取未定义的属性“ coords”

时间:2019-06-15 14:59:32

标签: google-maps-api-3 google-geolocation

我不知道我的代码有什么问题。我以为我会完全按照教程进行。不断显示错误“未捕获(承诺)TypeError:无法读取未定义的属性'coords'”

function initCoords() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(success, failure);
        } else {
          showError("Your browser does not support Geolocation!");
        }
      }
        function success(position)
        {
            var myLat = position.coords.latitude;
            var myLong = position.coords.longitude;
            //alert(myLat);
            var coords = new google.maps.LatLng(myLat,myLong);
            var mapOptions = {
                zoom:11,
                center: coords,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map(document.getElementById("map"), mapOptions);
            var marker = new google.maps.Marker({map:map, position:coords});    
        }
        function failure(){ }
  </script>
</head>
<body>
        <div id="map" onload="initCoords()" ></div>
</body>

0 个答案:

没有答案