mta
mta

Reputation: 1

Getting the LatLng from placed marker for browser fields

Tried the following scripts but the last section is not working. Help please.

// script type="text/javascript"

var myOptions = {
    zoom: 7,
    center: new google.maps.LatLng(-36.624345125527526,144.96459973437504),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    streetViewControl: false,
    draggableCursor: 'crosshair',
    draggingCursor: 'pointer'
    };
var map = new google.maps.Map(document.getElementById('map_canvas'),
    myOptions);
google.maps.event.addListener(map, 'click', function(e) {
    placeMarker(e.latLng, map);
    });

map.panTo(position);

// not working

google.maps.event.addListener(marker, 'dragend', function() {
    var point = marker.getPosition();
    document.getElementById("latitude").getElementsByTagName("input")[0].value = point.lat();
    document.getElementById("longitude").getElementsByTagName("input")[0].value = point.lng();
    });

// end of script


Tried using the following but nothing happen after the marker is drag. Seems like the listener is not listening to the 'dragend' event of the marker. Any clue? Other option to get the latLng after dragging marker on map?

// JS code

google.maps.event.addListener(marker, 'dragend', function(e) {
    var point = e.latLng;
    alert(marker.e.latLng);
})

// end code

The input elements part is not a choice as the scripts sit in an embedded placeholder of a web based application. So can't change that part.

Upvotes: 0

Views: 1299

Answers (1)

andresf
andresf

Reputation: 2061

You should be using the MouseEvent object passed by the dragend event for the marker. Like this:

google.maps.event.addListener(marker, 'dragend', function(e) {
    var point = e.latLng;
    document.getElementById("latitude").getElementsByTagName("input")[0].value = point.lat();
    document.getElementById("longitude").getElementsByTagName("input")[0].value = point.lng();
    });

I also am not sure why you're not just naming your input elements and accessing them directly...something like this:

document.getElementById("latitude_input").value = point.lat();
document.getElementById("longitude_input").value = point.lng();

Upvotes: 2

Related Questions