why does this:
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();
always return this:
Uncaught TypeError: Cannot read property 'coords' of undefined
on this line:
function codeLatLng(position) {
lat = position.coords.latitude;
enter code here
it shows me the map just not the marker with the address... I've also tried this: codeLatLng(position);
I moved the call to CodeLatLng() into the Initialize() function after you render the google map, and pass it the position argument. See comments above.
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.