javascript - Displaying lat lng wherever user clicks -
i want display latitude , longitude location user clicks on map wherever wants (i'm not using marker event), still shows. here code:
var mycenter=new google.maps.latlng(51.508742,-0.120850); function initialize() { var mapprop = { center:mycenter, zoom:5, maptypeid:google.maps.maptypeid.roadmap }; var map=new google.maps.map(document.getelementbyid("googlemap"),mapprop); var marker=new google.maps.marker({ position:mycenter, }); marker.setmap(map); var infowindow = new google.maps.infowindow(); google.maps.event.addlistener(map, 'click', function(event) { infowindow.setcontent(event.latlng.lat()+","+event.latlng.lng()); infowindow.open(map,marker); }); } google.maps.event.adddomlistener(window, 'load', initialize);
can help?
update position
-property of marker in click
-handler:
google.maps.event.addlistener(map, 'click', function(event) { marker.setposition(event.latlng); infowindow.setcontent(event.latlng.lat()+","+event.latlng.lng()); infowindow.open(map,marker); });
note: must latlng
, not latlng
or latlng
demo:
function initialize() { var mycenter = new google.maps.latlng(51.508742, -0.120850); var mapprop = { center: mycenter, zoom: 5, maptypeid: google.maps.maptypeid.roadmap }; var map = new google.maps.map(document.getelementbyid("googlemap"), mapprop); var marker = new google.maps.marker({ position: mycenter, }); marker.setmap(map); var infowindow = new google.maps.infowindow(); google.maps.event.addlistener(map, 'click', function(event) { marker.setposition(event.latlng); infowindow.setcontent(event.latlng.lat() + "," + event.latlng.lng()); infowindow.open(map, marker); }); google.maps.event.trigger(map,'click',{latlng:map.getcenter()}) } google.maps.event.adddomlistener(window, 'load', initialize);
html, body, #googlemap { height: 100%; margin: 0; padding: 0; }
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3"></script> <div id="googlemap"></div>
Comments
Post a Comment