【问题标题】:google maps API v3 put draggable maker谷歌地图 API v3 放置可拖动制造商
【发布时间】:2011-07-20 05:24:01
【问题描述】:

这是我的代码,

function initialize() {
  if (GBrowserIsCompatible()) {
    var city = 'battaramulla';
    var center = '';
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setUIToDefault();

    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(city, function (point) {
      if (!point) {
        alert(city+" not found :-(");
      } else {
            alert(city+" "+point+" found :-)");
            map.setCenter(point, 8);
      }
    });
}
}

我想将 drggable maker 添加到这个 city(point)。请帮我。谢谢。

【问题讨论】:

    标签: google-maps google-maps-markers draggable point


    【解决方案1】:

    你可以试试这个完整的例子……

    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">`enter code here`
    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('Cannot determine 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(-34.397, 150.644);
      var map = new google.maps.Map(document.getElementById('mapCanvas'), {
        zoom: 8,
        center: latLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      var marker = new google.maps.Marker({
        position: latLng,
        title: 'Point A',
        map: map,
        draggable: true
      });
    
      // Update current position info.
      updateMarkerPosition(latLng);
      geocodePosition(latLng);
    
      // Add dragging event listeners.
      google.maps.event.addListener(marker, 'dragstart', function() {
        updateMarkerAddress('Dragging...');
      });
    
      google.maps.event.addListener(marker, 'drag', function() {
        updateMarkerStatus('Dragging...');
        updateMarkerPosition(marker.getPosition());
      });
    
      google.maps.event.addListener(marker, 'dragend', function() {
        updateMarkerStatus('Drag ended');
        geocodePosition(marker.getPosition());
      });
    }
    
    // Onload handler to fire off the app.
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    </head>
    <body>
      <style>
      #mapCanvas {
        width: 500px;
        height: 400px;
        float: left;
      }
      #infoPanel {
        float: left;
        margin-left: 10px;
      }
      #infoPanel div {
        margin-bottom: 5px;
      }
      </style>
    
      <div id="mapCanvas"></div>
      <div id="infoPanel">
        <b>Marker status:</b>
        <div id="markerStatus"><i>Click and drag the marker.</i></div>
        <b>Current position:</b>
        <div id="info"></div>
        <b>Closest matching address:</b>
        <div id="address"></div>
      </div>
    </body>
    </html>
    

    【讨论】:

    • 在这里如何将我的预定义城市设置为点而不是经纬度。
    【解决方案2】:

    使用地理编码器查找最近的地址。

    function geocodePosition(pos) {
      geocoder.geocode({
        latLng: pos
      }, function(responses) {
        if (responses && responses.length > 0) {
          updateMarkerAddress(responses[0].formatted_address);
        } else {
          updateMarkerAddress('Cannot determine address at this location.');
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 2011-03-25
      • 2013-01-28
      • 1970-01-01
      相关资源
      最近更新 更多