谷歌地图设置位置?

时间:2012-10-16 06:43:15

标签: javascript google-maps-api-3

我有这段代码,显示当前位置在“LatLng”坐标中指定。但我想要的是有一个输入框,让我们说2个输入框“位置从”和“位置到”。我希望显示来自2个方框的给定输入值的当前位置。

这可以在Google Map V3中使用吗?

我想让mapOptions值成为框中的值输入。 怎么做?

这是我的代码。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBQ8OCC8En5vNHod25Ov3Qs5E1v7NPRSsg&sensor=true">
    </script>
    <script type="text/javascript">
      function initialize() {
                    // add mapOptions here to the values in the input boxes.
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 100.644),
          zoom: 2,
          mapTypeId: google.maps.MapTypeId.ROADMAP  
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
  </head>
  <body>

    From: <input type="text" name="From"><br>
    To: <input type="text" name="To"><br>

    <button type="button" onclick="initialize()">View example (map-simple.html)</button>

    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

由于

杰森

3 个答案:

答案 0 :(得分:4)

你应该使用geocoder

geocoder = new google.maps.Geocoder();      
var address = document.getElementById('address').value; //input box value
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
}
});  

Map Fiddle

答案 1 :(得分:0)

可能是这样的:

boxLat.value = map.getCenter().getLat() ;
boxLng.value = map.getCenter().getLng() ;

答案 2 :(得分:0)

您可以查看此地址的示例:https://developers.google.com/maps/documentation/javascript/demogallery

在这里,您可以找到您的问题的答案,以及您可能需要的更高级的技术。

电贺!

相关问题