如何从html表单获取纬度经度值到谷歌地图

How to get latitude longitude value from html form to google map

本文关键字:经度 谷歌地图 纬度 获取 html 表单      更新时间:2023-09-26
<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>

我在html的标题中有相关数据

html正文中,我有一个表单

 <form>
        <input type="number" name="latitude" >
        <input type="number" name="longitude" >
        <input type="button" onclick="ShowMap(latitude.value,longitude.value)" value="ShowMap"/>
    </form>

它需要纬度经度具有要绘制的图形的输入,但是,现在我得到的功能图中的值没有显示,我做错了什么吗

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

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

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

工作小提琴

代码片段:

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>

请尝试以下方法:

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();

在这里演示