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?
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.
If you want to stay in the world of google maps API, and assuming you have the map variable you can use,
or you could use,
...
For people looking for a solution in the future, Google now has a Geolocation example on the Google Maps page > https://developers.google.com/maps/documentation/javascript/examples/map-geolocation