Drag and drop Marker events on google maps

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

 

    <script type="text/javascript">  

    var geocoder = new google.maps.Geocoder();  

      

    function geocodePosition(pos) {  

      geocoder.geocode({  

        latLng: pos  

      }, function(responses) {  

        if (responses && responses.length > 0) {  

          updateMarkerAddress(responses[0].formatted_address);  

        } else {  

          updateMarkerAddress('Unable to determine the address at this location.');  

        }  

      });  

    }  

      

    function updateMarkerStatus(str) {  

      document.getElementById('markerStatus').innerHTML = str;  

    }  

      

    function updateMarkerPosition(latLng) {  

      document.getElementById('info').innerHTML = [  

        latLng.lat(),  

        latLng.lng ()  

      ].join(', ');  

    }  

      

    function updateMarkerAddress(str) {  

      document.getElementById('address').innerHTML = str;  

    }  

      

    function initialize() {  

      var latLng = new google.maps.LatLng(31.1933370274183, 121.43890661621094);  

      var map = new google.maps.Map(document.getElementById('mapCanvas'), {  

        zoom: 11,  

        center: latLng,  

        mapTypeId: google.maps.MapTypeId.ROADMAP  

      });  

      var marker = new google.maps.Marker ({  

        position: latLng,  

        title: 'Point A',  

        map: map,  

        draggable: true  

      });  

      google.maps.event.addListener(marker, "mouseover", function() {  

      

                    marker.setImage('logo.png');  

      

                    });   

      google.maps.event.addListener(marker, "mouseout", function() {  

      

                    marker.setImage('logo.png');  

                      

                    });   

      // update the current location information  

      updateMarkerPosition(latLng);  

      geocodePosition(latLng);  

        

      // Add drag event listener  

      google.maps.event.addListener(marker, 'dragstart', function() {  

        updateMarkerAddress('Searching...');  

      });  

        

      google.maps.event.addListener(marker, 'drag', function() {  

        updateMarkerStatus('Searching...');  

        updateMarkerPosition(marker.getPosition());  

      });  

        

      google.maps.event.addListener(marker, 'carrying', function() {  

        updateMarkerStatus('Search end');  

        geocodePosition(marker.getPosition());  

      });  

    }  

      

    // Load the application.  

    google.maps.event.addDomListener(window, 'load', initialize);  

    </script>  

</head>

<body>

<div id="mapCanvas" style="height: 600px"></div>

<div id="markerStatus">1</div>

<div id="info">2</div>

<div id="address">3</div>

</body>

</html>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327028122&siteId=291194637