How to get only one marker in the Google Maps API with JavaScript -
my code:
<script src="http://maps.googleapis.com/maps/api/js"> </script> <script> var map; var mycenter=new google.maps.latlng(51.508742,-0.120850); function initialize() { var mapprop = { center:mycenter, zoom:5, maptypeid:google.maps.maptypeid.roadmap }; map = new google.maps.map(document.getelementbyid("googlemap"),mapprop); google.maps.event.addlistener(map, 'click', function(event) { placemarker(event.latlng); }); } function placemarker(location) { var marker = new google.maps.marker({ position: location, map: map, }); var infowindow = new google.maps.infowindow({ content: 'latitude: ' + location.lat() + '<br>longitude: ' + location.lng() }); infowindow.open(map,marker); } google.maps.event.adddomlistener(window, 'load', initialize); </script> </head> <body> <div id="googlemap" style="width:500px;height:380px;"></div>
ive got every klick new marker. how can remove other if click again?
btw: readed :google maps how show 1 marker?
wanted use it, doenst work (in console:tcurrentposition() , watchposition() deprecated on insecure origins, , support removed in future. should consider switching application secure origin, such https. see https://goo.gl/rsttgz more details.)
thank :)
one option if ever want 1 marker, create 1 , move required.
code snippet:
var map; var mycenter = new google.maps.latlng(51.508742, -0.120850); var marker; var infowindow; function initialize() { var mapprop = { center: mycenter, zoom: 5, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map(document.getelementbyid("googlemap"), mapprop); google.maps.event.addlistener(map, 'click', function(event) { placemarker(event.latlng); }); } function placemarker(location) { if (!marker || !marker.setposition) { marker = new google.maps.marker({ position: location, map: map, }); } else { marker.setposition(location); } if (!!infowindow && !!infowindow.close) { infowindow.close(); } infowindow = new google.maps.infowindow({ content: 'latitude: ' + location.lat() + '<br>longitude: ' + location.lng() }); infowindow.open(map, marker); } google.maps.event.adddomlistener(window, 'load', initialize);
html, body, #googlemap { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="googlemap"></div>
Comments
Post a Comment