javascript - Draggable marker in map -
i'm example map shows current location click of button shows lat,long , need little change on map
changes :
1 - marker on map after click button draggable new lat long , show address , in fact marker fixed on center map , map draggable new address , new lat,long
my code :
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map = null; function showlocation() { // one-shot position request. navigator.geolocation.getcurrentposition(callback); } function callback(position) { var lat = position.coords.latitude; var lon = position.coords.longitude; document.getelementbyid('default_latitude').value = lat; document.getelementbyid('default_longitude').value = lon; var latlong = new google.maps.latlng(lat, lon); var marker = new google.maps.marker({ position: latlong }); marker.setmap(map); map.setzoom(16); map.setcenter(marker.getposition()); } google.maps.event.adddomlistener(window, 'load', initmap); function initmap() { var mapoptions = { center: new google.maps.latlng(0, 0), zoom: 1, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map(document.getelementbyid("map-canvas"), mapoptions); } </script>
<input type="button" class="btn pull-right map-btn" value="btn " onclick="javascript:showlocation()" /> <div id="map-canvas" style="height: 300px"></div> <input type="text" id="default_latitude" placeholder="latitude"/> <input type="text" id="default_longitude" placeholder="longitude"/>
<script type="text/javascript"> var map = null; var marker; function showlocation() { // one-shot position request. navigator.geolocation.getcurrentposition(callback); } function callback(position) { if (marker != null) { marker.setmap(null); } var geocoder = new google.maps.geocoder(); var lat = position.coords.latitude; var lon = position.coords.longitude; document.getelementbyid('default_latitude').value = lat; document.getelementbyid('default_longitude').value = lon; var latlong = new google.maps.latlng(lat, lon); marker = new google.maps.marker({ position: latlong, draggable:true }); marker.setmap(map); map.setzoom(16); map.setcenter(marker.getposition()); google.maps.event.addlistener(marker, 'dragend', function() { geocoder.geocode({'latlng': marker.getposition()}, function(results, status) { if (status == google.maps.geocoderstatus.ok) { if (results[0]) { $('#default_latitude').val(marker.getposition().lat()); $('#default_longitude').val(marker.getposition().lng()); `enter code here` } } }); }); } google.maps.event.adddomlistener(window, 'load', initmap); function initmap() { var mapoptions = { center: new google.maps.latlng(0, 0), zoom: 1, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map(document.getelementbyid("map-canvas"), mapoptions); } </script>
Comments
Post a Comment