Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latitude/longitude coordinates from a Google map using a marker

I just wondered if anyone knew of a simple script available that will do the following:

Load a google map in view, when clicked it displays a marker that will save the lat and long values to a variable?

Does anyone know if something like this already exists in PHP?

Thanks in advance

like image 655
Zabs Avatar asked Jan 25 '12 10:01

Zabs


People also ask

How do I find the latitude and longitude of a marker on Google Maps?

addListener(myMarker, 'dragend', function(evt){ document. getElementById('current'). innerHTML = '<p>Marker dropped: Current Lat: ' + evt. latLng.

Can I put a marker on Google Maps?

You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.


1 Answers

Perhaps you are looking for something similar to this Latitude-Longitude Finder Tool. The example code is was API v2. Below is trimmed down version of the code using Google Maps API v3:

var latlng = new google.maps.LatLng(51.4975941, -0.0803232);
var map = new google.maps.Map(document.getElementById('map'), {
    center: latlng,
    zoom: 11,
    mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
    position: latlng,
    map: map,
    title: 'Set lat/lon values for this property',
    draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(a) {
    console.log(a);
    // bingo!
    // a.latLng contains the co-ordinates where the marker was dropped
});

Demo

Explanation: you must set the draggable property of the marker to true. You can then hook a callback function to that marker's dragend event; the new co-ordinates are passed to the function and you can assign them to a JavaScript variable or form field.

like image 95
Salman A Avatar answered Oct 17 '22 20:10

Salman A