繁体   English   中英

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

[英]How to get latitude longitude value from html form to google map

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

演示在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM