Google地图设置的位置? [英] Google Map set location?

查看:114
本文介绍了Google地图设置的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码显示当前位置是在LatLng坐标中指定的。但我想要的是有一个输入框,可以说2个输入框位置从和位置到。我希望显示指定的2个框中给定输入值的当前位置。



Google Map V3中有这种可能吗?



我想让mapOptions的值成为值输入在箱子里。
如何做到这一点?



这是我的代码。

 <!DOCTYPE html> 
< html>
< head>
< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no/>
< style type =text / css>
html {height:100%}
body {height:100%;保证金: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>
函数initialize(){
//将mapOptions添加到输入框中的值。
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>

来自:< input type =textname =From>< br>
收件人:< input type =textname =To>< br>

< button type =buttononclick =initialize()>查看示例(map-simple.html)< / button>

< div id =map_canvasstyle =width:100%; height:100%>< / div>
< / body>
< / html>

谢谢

Jason

解决方案

您应该为>使用 geocoder $ b

  geocoder = new google.maps.Geocoder(); 
var address = document.getElementById('address')。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。位置
});
}
});

地图小提琴


I have this code that shows the current location as being specified in the "LatLng" coordinates. But what I want is have have an input boxes, lets say 2 input boxes "location from" and "location to." I wish to show the current location of the given input values from the 2 boxes as being indicated.

Is that possible in Google Map V3?

I want to have the mapOptions values will be the values inputs in the boxes. How to do that?

Here is my code.

<!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>

Thanks

Jason

解决方案

you should use geocoder for that

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

这篇关于Google地图设置的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆