javascript - Not able to insert a map -
i find code allows city information. how can implement simple map https://developers.google.com/maps/documentation/javascript/examples/map-simple? see map on app. function same write on code.
<!doctype html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>reverse geocoding</title> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var geocoder; if (navigator.geolocation) { navigator.geolocation.getcurrentposition(successfunction, errorfunction); } function successfunction(position) { var lat = position.coords.latitude; var lng = position.coords.longitude; codelatlng(lat, lng) } function errorfunction(){ alert("geocoder failed"); } function initialize() { geocoder = new google.maps.geocoder(); } function codelatlng(lat, lng) { var latlng = new google.maps.latlng(lat, lng); geocoder.geocode({'latlng': latlng}, function(results, status) { if (status == google.maps.geocoderstatus.ok) { console.log(results) if (results[1]) { //formatted address alert(results[0].formatted_address) //find country name (var i=0; i<results[0].address_components.length; i++) { (var b=0;b<results[0].address_components[i].types.length;b++) { //there different types might hold city admin_area_lvl_1 in come cases looking sublocality type more appropriate if (results[0].address_components[i].types[b] == "administrative_area_level_1") { //this object looking city= results[0].address_components[i]; break; } } } //city data alert(city.short_name + " " + city.long_name) } else { alert("no results found"); } } else { alert("geocoder failed due to: " + status); } }); } </script> </head> <body onload="initialize()"> </body> </html>
use code in link https://developers.google.com/maps/documentation/javascript/examples/map-geolocation.
then can copy function codelatlng on new code , invoke it.
Comments
Post a Comment