Retrieve latitude and longitude of a draggable pin via Google Maps API V3 -
i explain. managed have draggable pin on map. want retrieve coordinates of point , put them 2 fields: latitude , longitude. these coordinates later send sql table via php. here an example of intend do, instead of several pins, it's 1 , it's draggable. problem is: i'm not able display coordinates of initial point. , of course when user moves pin, want coordinates change in fields. hope made myself clear. did wrong? should use geocoding service?
here goes js:
<script type="text/javascript"> var map; function initialize() { var mylatlng = new google.maps.latlng(40.713956,-74.006653); var myoptions = { zoom: 8, center: mylatlng, maptypeid: google.maps.maptypeid.roadmap } map = new google.maps.map(document.getelementbyid("map_canvas"), myoptions); var marker = new google.maps.marker({ draggable: true, position: mylatlng, map: map, title: "your location" }); google.maps.event.addlistener(marker,'click',function(overlay,point){ document.getelementbyid("latbox").value = lat(); document.getelementbyid("lngbox").value = lng(); }); } </script>
and html:
<html> <body onload="initialize()"> <div id="map_canvas" style="width:50%; height:50%"></div> <div id="latlong"> <p>latitude: <input size="20" type="text" id="latbox" name="lat" ></p> <p>longitude: <input size="20" type="text" id="lngbox" name="lng" ></p> </div> </body> </html>
either of these work
google.maps.event.addlistener(marker, 'click', function (event) { document.getelementbyid("latbox").value = event.latlng.lat(); document.getelementbyid("lngbox").value = event.latlng.lng(); }); google.maps.event.addlistener(marker, 'click', function (event) { document.getelementbyid("latbox").value = this.getposition().lat(); document.getelementbyid("lngbox").value = this.getposition().lng(); });
you might consider using dragend event also
google.maps.event.addlistener(marker, 'dragend', function (event) { document.getelementbyid("latbox").value = this.getposition().lat(); document.getelementbyid("lngbox").value = this.getposition().lng(); });
Comments
Post a Comment