Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Location On Google Map

Tags:

I'm having some trouble when i want to get current location.

This is my first time using this GM API, and there are so much things i don't understand.

Here is my code, and i want

var geocoder = new google.maps.Geocoder();  function initialize() {   var latLng = new google.maps.LatLng(-7.7801502, 110.3846387);   var map = new google.maps.Map(document.getElementById('mapCanvas'), {     zoom: 15,     center: latLng,     mapTypeId: google.maps.MapTypeId.ROADMAP   });    var marker = new google.maps.Marker({     position: latLng,     title: 'Ambarrukmo Plaza Yogyakarta',     map: map,     draggable: true   }); } 

The problem is, i want to change -7.7801502 and 110.3846387 value automatically based on user's current position. Can i do that?

Thanks before for your help and explanation.

Another question : -> What if i'm going to change those value based on a device embedded with GPS?

like image 319
Arvid Theodorus Avatar asked Dec 08 '12 18:12

Arvid Theodorus


1 Answers

You can't get current user's location with Google Maps. However, if you get Google Maps with Google Loader, you can use the google.loader.CurrentLocation to get a location based on IP.

An other way is to use the HTML5 GeoLocation API.

function getLocation() {   if (navigator.geolocation) {     navigator.geolocation.getCurrentPosition(showPosition);   } else {     alert("Geolocation is not supported by this browser.");   } } function showPosition(position) {   var lat = position.coords.latitude;   var lng = position.coords.longitude;   map.setCenter(new google.maps.LatLng(lat, lng)); } 
like image 61
Alexandre Ardhuin Avatar answered Sep 26 '22 21:09

Alexandre Ardhuin