Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

javascript - 我无法获得当前地址(i am unable to get the current address)

 <script type="text/javascript"> var watchId, geocoder, startLat, startLong; var start = 1; window.onload = function() { if (navigator.geolocation) { watchId = navigator.geolocation.watchPosition(onSuccess, onError,{maximumAge:60*1000, timeout:5*60*1000, enableHighAccuracy:true}); } } function onSuccess(position) { var currentLat = position.coords.latitude; var currentLong = position.coords.longitude; if (start == 1) { startLat = currentLat; startLong = currentLong; start = 0; } var geocoder = new google.maps.Geocoder(); var latlong = new google.maps.LatLng(currentLat, currentLong); geocoder.geocode({'latLng':latlong}, function(results, status){ if (status == google.maps.GeocoderStatus.OK) { if (results) { document.getElementById("location").innerHTML = "You are now near " + results[1].formatted_address; } } else alert("Could not get the geolocation information"); }); var mapOptions = { center: new google.maps.LatLng(startLat, startLong), zoom: 10, mapTypeId: google.maps.MapTypeId. }; var map = new google.maps.Map(document.getElementById("mapArea"), mapOptions); var marker = new google.maps.Marker({position: latlong,map: map}); } function onError(error) { switch(error.code) { case PERMISSION_DENIED: alert("User denied permission"); break; case TIMEOUT: alert("Geolocation timed out"); break; case POSITION_UNAVAILABLE: alert("Geolocation information is not available"); break; default: alert("Unknown error"); break; } } </script> 
 <!DOCTYPE html> <html> <head> <title>location</title> <script src="https://maps.googleapis.com/maps/api/js"></script> </head> <body> <div id="location"></div> <div id="mapArea" style="height:500px; width:500px"></div> </body> </html> 

 <!DOCTYPE html> <html> <head> <title>location</title> <script src="https://maps.googleapis.com/maps/api/js"></script> <script type="text/javascript"> var watchId, geocoder, startLat, startLong; var start = 1; window.onload = function() { if (navigator.geolocation) { watchId = navigator.geolocation.watchPosition(onSuccess, onError,{maximumAge:60*1000, timeout:5*60*1000, enableHighAccuracy:true}); } } function onSuccess(position) { var currentLat = position.coords.latitude; var currentLong = position.coords.longitude; if (start == 1) { startLat = currentLat; startLong = currentLong; start = 0; } var geocoder = new google.maps.Geocoder(); var latlong = new google.maps.LatLng(currentLat, currentLong); geocoder.geocode({'latLng':latlong}, function(results, status){ if (status == google.maps.GeocoderStatus.OK) { if (results) { document.getElementById("location").innerHTML = "You are now near " + results[1].formatted_address; } } else alert("Could not get the geolocation information"); }); var mapOptions = { center: new google.maps.LatLng(startLat, startLong), zoom: 10, mapTypeId: google.maps.MapTypeId. }; var map = new google.maps.Map(document.getElementById("mapArea"), mapOptions); var marker = new google.maps.Marker({position: latlong,map: map}); } function onError(error) { switch(error.code) { case PERMISSION_DENIED: alert("User denied permission"); break; case TIMEOUT: alert("Geolocation timed out"); break; case POSITION_UNAVAILABLE: alert("Geolocation information is not available"); break; default: alert("Unknown error"); break; } } </script> </head> <body> <div id="location"></div> <div id="mapArea" style="height:500px; width:500px"></div> </body> </html> 

 <script src="https://maps.googleapis.com/maps/api/js"></script> <script type="text/javascript"> var watchId, geocoder, startLat, startLong; var start = 1; window.onload = function() { if (navigator.geolocation) { watchId = navigator.geolocation.watchPosition(onSuccess, onError,{maximumAge:60*1000, timeout:5*60*1000, enableHighAccuracy:true}); } } function onSuccess(position) { var currentLat = position.coords.latitude; var currentLong = position.coords.longitude; if (start == 1) { startLat = currentLat; startLong = currentLong; start = 0; } var geocoder = new google.maps.Geocoder(); var latlong = new google.maps.LatLng(currentLat, currentLong); geocoder.geocode({'latLng':latlong}, function(results, status){ if (status == google.maps.GeocoderStatus.OK) { if (results) { document.getElementById("location").innerHTML = "You are now near " + results[1].formatted_address; } } else alert("Could not get the geolocation information"); }); var mapOptions = { center: new google.maps.LatLng(startLat, startLong), zoom: 10, mapTypeId: google.maps.MapTypeId. }; var map = new google.maps.Map(document.getElementById("mapArea"), mapOptions); var marker = new google.maps.Marker({position: latlong,map: map}); } function onError(error) { switch(error.code) { case PERMISSION_DENIED: alert("User denied permission"); break; case TIMEOUT: alert("Geolocation timed out"); break; case POSITION_UNAVAILABLE: alert("Geolocation information is not available"); break; default: alert("Unknown error"); break; } } </script> 
 <!DOCTYPE html> <html> <head> <title>current location</title> </head> <body> <div id="location"></div> <div id="mapArea" style="height:500px; width:500px"></div> </body> </html> 

i am unable to get the current location

(我无法获取当前位置)

  ask by Nagabhushan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...