How to tell the user to indicate their position in the Google maps application

I want the user to mark his position in our Google Maps application, and then save it in our database, after which he can be shown in our Google Maps application next time.

+3
source share
2 answers

If you want to save the position in which the user clicked, you can use the "click" listener to get the latitude and longitude of the click with this code, and 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 the Google Maps API v2. Version 3 should have something similar.

+1
source

- , . , , .

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();
}

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

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


All Articles