【问题标题】:Get lat and long onclick of map which is in iframe获取 iframe 中地图的经纬度 onclick
【发布时间】:2018-08-30 10:24:15
【问题描述】:

我有在服务器上设置并基于谷歌地图的地图。要将其嵌入到我的网页中,我使用了 iframe。现在我需要在点击地图时获得经纬度,而不是将数据写入我的数据库。我怎样才能得到它?

这是我的 iframe

<iframe src="https://my_domain/projects/?first=7&second=123&third=567" width="100%" height="350px" align="left" id="inneriframe" scrolling="no"></iframe>

这里没有我通常用于谷歌地图的初始化或其他函数,这就是为什么不能通过函数获取它的原因。

【问题讨论】:

  • 关注此Link。对你有帮助。
  • @sagarshinde 但情况不同。我没有任何按钮,也没有从 div 中获取数据。

标签: javascript google-maps iframe


【解决方案1】:

您可以在 window.load 上初始化地图并使用事件 addListener 获取值。

var map;
    function initialize() {
        var myLatlng = new google.maps.LatLng(24.18061975930,79.36565089010);
        var myOptions = {
            zoom:7,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("gmap"), myOptions);
        // marker refers to a global variable
        marker = new google.maps.Marker({
            position: myLatlng,
            map: map
        });

        google.maps.event.addListener(map, "click", function(event) {
            // get lat/lon of click
            var clickLat = event.latLng.lat();
            var clickLon = event.latLng.lng();

           alert(clickLat.toFixed(5));
           alert(clickLon.toFixed(5));

              var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(clickLat,clickLon),
                    map: map
                 });
        });
}   

window.onload = function () { initialize() };

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 2011-06-21
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多