Google Map - Current Location

I would like to get the address bar of the user's current location. Is it possible?

Many thanks. Relations

+4
source share
5 answers

if you are trying to get a street address !

include both JS and MAP Api Key, save everything together from the wizard

my code looks like this: (also use a bit of jQuery)

var geocoder = null; var lat = null; var lng = null; $(function() { if (GBrowserIsCompatible()) { if (google.loader.ClientLocation && // i used this here cause not always //this retrieve the correct LAT & LON // so set it manually google.loader.ClientLocation.address.country_code == "IT" ) { lat = google.loader.ClientLocation.latitude; lng = google.loader.ClientLocation.longitude; } else { //alert( 'Insert Your Latitude & longitude manually!' ); lat = '42.464826'; lng = '14.214095'; } //creat the full LAT + LON points = new GLatLng( lat , lng ); //get the Street Address get_address(points); } }); function get_address(latlng) { if (latlng) { geocoder = new GClientGeocoder(); geocoder.getLocations(latlng, function(addresses) { if(addresses.Status.code != 200) { alert("reverse geocoder failed to find an address for " + latlng.toUrlValue()); } else { address = addresses.Placemark[0]; var myHtml = address.address; $('#address').text(myHtml); } }); } }; 

peraphs reading this link can also help:

Get street address in lat / long pair

+1
source

the user's location is available in google.loader.ClientLocation (this will contain an assumption based on the client's IP address, if the IP is known)

google.loader.ClientLocation

When an application uses the AJAX API Loader, the loader attempts to determine the location of the client based on its IP address. If this process is successful, the location of the client, metro, is provided in the google.loader.ClientLocation property. If the process does not find a match, this property is null.

http://code.google.com/apis/ajax/documentation/#ClientLocation

+2
source

If you need something more accurate than IP geolocation, you can use the W3C Geolocation API in supported browsers (specifically Firefox 3.5 and Mobile Safari) to request the latitude and longitude of the user, and then use Google reverse geocoding on the client side . to guess the approximate address of the user. (Code samples for both of these steps are included in my test page .)

If you go along this route, be sure to explain to your users why you request their location and how you use their information (for example, that you do not store it until they submit the form), because this is technically required by the API and because some users will be squeezed out by how easily you can guess their street address.

+1
source

I was looking for how to display the current location and destination. To achieve this, here is what I did:

Note * replace: YOUR_DESTINATION_ADDRESS with an address in the format: "147 + Wyndham + Street + N +, + Suite + 206, + Guelph, + On, + N1H + 4E9"

 <div id="Map" style="width: 100%; height: 600px;"> </div> <script type="text/javascript"> $(function(){ var zoom = 11; function success(position) { var LAT = position.coords.latitude; var LONG = position.coords.longitude; $('#Map').append('<p>Current Latitude: ' + LAT + '<br/>Current Logitude: ' + LONG +'<br/><br/>Note* This may not display your exact location, but just your city. Works better on mobile devices</p>'); var map = '<iframe style="width: 100%; height: 600px;" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.ca/maps?source=s_d&saddr=' + LAT +',+' + LONG +'&daddr=**YOUR_DESTINATION_ADDRESS**&ie=UTF8&t=hz=' + zoom + '&output=embed"></iframe>'; $('#Map').append(map); } function error(){ /*Error Code*/ } function MakeMap(){ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error); } } MakeMap(); }); </script> 
0
source

Yes, you can achieve the use of html 5 and Google API 3, in this code you need to change your APIKey, and map-canvas is an element in the html package

  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=yourAPIKey&sensor=false"></script> <script type="text/javascript"> var lat; var lng; function initialize() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } } function showPosition(position) { lat = position.coords.latitude; lng = position.coords.longitude; var myLatlng = new google.maps.LatLng(lat, lng); var myTittle = "My Current Location! Lat: " + lat + "lng: " + lng; var marker = new google.maps.Marker({ position: myLatlng, title: myTittle }); var mapOptions = { center: myLatlng, zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); marker.setMap(map); } google.maps.event.addDomListener(window, 'load', initialize); </script> 
0
source

Source: https://habr.com/ru/post/1304640/


All Articles