lovespring
lovespring

Reputation: 19569

How to let user point their position in my Google maps application?

I want let user mark their position in our google maps application and then save it to our database, then it can be showed in our google maps application in the next time.

Upvotes: 3

Views: 563

Answers (2)

Galen
Galen

Reputation: 30170

An options is to try geolocation first and let the browser pinpoint them. If the browser doesn't support geolocation or an error occurs you can fall back onto them manually adding their position.

var geolocation = null; // holds the latlng object
navigator.geolocation.getCurrentPosition( geolocation_success_init, geolocation_error_init, {enableHighAccuracy: false, timeout: 10000} );

function geolocation_success_init( position ) {
    geolocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
    initialize_map();
}
function geolocation_error_init( error ){
    initialize_map();
}

when you create your map check for geolocation

if ( geolocation ) {
    marker = new google.maps.Marker({
        position: geolocation,
        map:.map,
        title: "Your Location"
    });
}

Upvotes: 1

DMKing
DMKing

Reputation: 1715

If you want to save the position where the user clicked you can use a "click" listener to get the latitude and longitude of the click with code like this then send it to your server using an Ajax style call where it can be stored in the database.

var clickListener = GEvent.addListener(map,"click",
    function (overlay,latlng,overlaylatlng) {
        alert(latlng);
});

This code is for v2 of the Google Maps API. Version 3 should have something similar.

Upvotes: 1

Related Questions