为什么会这样:
function initCoords() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initialize, locationError);
} else {
showError("Your browser does not support Geolocation!");
}
}
initCoords();
function locationError(){
alert('"Your browser does not support Geolocation!"');
}
function initialize(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
var acc = position.coords.accuracy;
geocoder = new google.maps.Geocoder();
// Debugging
console.log(position.coords);
console.log("Accuracy: "+acc+"\nLatitude: "+lat+"\nLongitude: "+lon);
var latlng = new google.maps.LatLng(lat,lon);
// Google Maps API
var myLatlng = new google.maps.LatLng(lat,lon);
var mapOptions = {
center: latlng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}
function codeLatLng(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
codeLatLng();
总是回复:
Uncaught TypeError: Cannot read property 'coords' of undefined
在这一行:
function codeLatLng(position) {
lat = position.coords.latitude;
enter code here
它向我显示的地图不是带地址的标记...... 我也试过这个: codeLatLng(位置);
答案 0 :(得分:1)
在渲染谷歌地图后,我将调用CodeLatLng()移到了Initialize()函数中,并将其传递给position参数。见上面的评论。
<html>
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
function initCoords() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initialize, locationError);
} else {
showError("Your browser does not support Geolocation!");
}
}
initCoords();
function locationError(){
alert('"Your browser does not support Geolocation!"');
}
function initialize(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
var acc = position.coords.accuracy;
geocoder = new google.maps.Geocoder();
// Debugging
console.log(position.coords);
console.log("Accuracy: "+acc+"\nLatitude: "+lat+"\nLongitude: "+lon);
var latlng = new google.maps.LatLng(lat,lon);
// Google Maps API
var myLatlng = new google.maps.LatLng(lat,lon);
var mapOptions = {
center: latlng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
codeLatLng( position );
}
function codeLatLng(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
</script>
</head>
<body onload="">
<div id="map_canvas" style="width: 400px; height: 400px;"></div>
</body>
</html>