javascript 地理位置未捕获类型错误:无法读取未定义的属性“坐标”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13406914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geolocation Uncaught TypeError: Cannot read property 'coords' of undefined
提问by robabby
I'm messing with the Google Maps API with Geolocation. Everything is working as expected, however I keep getting this error in my console:
我正在使用带有地理定位功能的 Google Maps API。一切都按预期工作,但是我在控制台中不断收到此错误:
Uncaught TypeError: Cannot read property 'coords' of undefined
Uncaught TypeError: Cannot read property 'coords' of undefined
Here is my Geo Check:
这是我的地理检查:
// Does this browser support geolocation?
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initialize, locationError);
} else {
showError("Your browser does not support Geolocation!");
}
My Success Handler:
我的成功处理程序:
function initialize(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var acc = position.coords.accuracy;
// Debugging
console.log(position.coords);
console.log("Accuracy: "+acc+"\nLatitude: "+lat+"\nLongitude: "+lon);
// Google Maps API
var myLatlng = new google.maps.LatLng(lat,lon);
var mapOptions = {
center: new google.maps.LatLng(lat, lon),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
}
I then initialize the map in my body tag <body id="map" onload="initialize()">
然后我在我的身体标签中初始化地图 <body id="map" onload="initialize()">
The map renders fine, everything is working as expected. When I log position.coords
to my console, I get a clean readout. Why do I keep receiving this error?
地图渲染良好,一切都按预期工作。当我登录position.coords
到我的控制台时,我得到了一个干净的读数。为什么我一直收到这个错误?
Google and SO searches rendered no results...
谷歌和 SO 搜索没有结果......
Cheers
干杯
回答by Tamás Pap
When document is loaded, the initialize method is called with no argument. That's why you get the error.
加载文档时,将调用 initialize 方法,不带参数。这就是你得到错误的原因。
Try this way instead:
试试这种方式:
function initCoords() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initialize, locationError);
} else {
showError("Your browser does not support Geolocation!");
}
}
and in your html code:
并在您的 html 代码中:
<body id="map" onload="initCoords()">
<body id="map" onload="initCoords()">
Keep the initialize
function as is.
保持initialize
功能不变。
回答by lostsource
The error is probably here
错误可能在这里
<body id="map" onload="initialize()">
You're calling the initialize function with no parameters. Therefore the 'position' parameter is undefined
您正在调用不带参数的 initialize 函数。因此“位置”参数未定义
function initialize(position) {
var lat = position.coords.latitude;
Its as if you called
就好像你打电话
var lat = undefined.coords.latitude
回答by pingle60
If you are getting the error on google maps in cordova/phonegap on initialization before using the map object, make sure the google map html data-lat and data-long properties exist.
如果在使用地图对象之前在初始化时在cordova/phonegap 中的谷歌地图上遇到错误,请确保谷歌地图html data-lat 和data-long 属性存在。
Here is a sample,
这是一个示例,
<div class="widget uib_w_20 maps-size d-margins" data-uib="media/google_maps" data-ver="0" data-rpath="null" data-zoom="8" id="google-maps" data-center-auto="true" data-lat="41.309697" data-long="-72.9370447">
<div class="widget uib_w_20 maps-size d-margins" data-uib="media/google_maps" data-ver="0" data-rpath="null" data-zoom="8" id="google-maps " data-center-auto="true" data-lat="41.309697" data-long="-72.9370447">
without data-lat and data-long, and testing as web app, will fail during google map initialization
没有数据纬度和数据长度,并作为网络应用程序测试,将在谷歌地图初始化期间失败