如何从html格式获取纬度经度值到谷歌地图

时间:2015-11-13 05:39:07

标签: javascript html google-maps

__1

我在 html

的标题中有相关数据

html 正文中,我有一个表单

<head>
    <script
            src="http://maps.googleapis.com/maps/api/js">
    </script>

    <script>
        function ShowMap(latitude,longitude) {
            console.log("This is latitude :" + latitude);
            console.log("This is longitude :" + longitude);

            var myCenter = new google.maps.LatLng(latitude, longitude);
            var marker;

            function initialize() {
                var mapProp = {
                    center: myCenter,
                    zoom: 5,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

                var marker = new google.maps.Marker({
                    position: myCenter,
                    animation: google.maps.Animation.BOUNCE
                });

                marker.setMap(map);
            }

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


    </script>
</head>

其中纬度经度具有要绘制图形的输入但是,现在我得到的函数图中的值没有显示在那里我正在做的事情

2 个答案:

答案 0 :(得分:1)

您需要直接调用初始化例程,这不起作用:

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

由于窗口load事件已经触发。

working fiddle

代码段

function ShowMap(latitude, longitude) {
  console.log("This is latitude :" + latitude);
  console.log("This is longitude :" + longitude);

  var myCenter = new google.maps.LatLng(latitude, longitude);
  var marker;

  function initialize() {
    var mapProp = {
      center: myCenter,
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    var marker = new google.maps.Marker({
      position: myCenter,
      animation: google.maps.Animation.BOUNCE,
      title: myCenter.toUrlValue(6)
    });

    marker.setMap(map);
  }

  initialize();
}
html,
body,
#googleMap {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<form>
  <input type="number" name="latitude" value="42" />
  <input type="number" name="longitude" value="-72" />
  <input type="button" onclick="ShowMap(latitude.value,longitude.value)" value="ShowMap" />
</form>
<div id="googleMap"></div>

答案 1 :(得分:0)

请尝试这个:

function ShowMap(latitude, longitude) {
    console.log("This is latitude :" + latitude);
    console.log("This is longitude :" + longitude);

    var myCenter = new google.maps.LatLng(latitude, longitude);
    var marker;

    function initialize() {
        var mapProp = {
            center: myCenter,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

        var marker = new google.maps.Marker({
            position: myCenter,
            animation: google.maps.Animation.BOUNCE
        });

        marker.setMap(map);
    }

    initialize();

DEMO HERE