单击Google地图时添加标记

时间:2014-06-17 15:47:21

标签: javascript php google-maps gis

我制作了一个包含Google地图的php页面。我的目标是在用户单击地图时添加标记,并在文本框中显示标记的坐标。这是我的javascript:

<script type="text/javascript">
        function tampil_peta()
        {
            var div_peta = document.getElementById('kanvas');
            var tengah = new google.maps.LatLng(-8.801502,115.174794);
            var options = 
            {
                center : tengah,
                zoom : 14,
                mapTypeId : google.maps.MapTypeId.ROADMAP
            }

            //membuat objek peta Google Maps
            var google_map = new google.maps.Map(div_peta,options);

            google.maps.event.addListener(google_map, "click", function (e) 
            {
                var latitude = e.latLng.lat();
                var longitude = e.latLng.lng();
                var location = new google.maps.LatLng(latitude,longitude);

                document.getElementById('lattext').value = latitude;
                document.getElementById('lngtext').value = longitude;
            });
        }
        function add_marker(location)
        {
            var marker = new google.maps.Marker({
                position : location,
                title : "Titik A",
                draggable : false,
                map : google_map
            });

        }

    </script>

这是包含2个文本框的表单代码,该文本框应自动填充标记的纬度和经度数据:

<form id="koordinat_a" nama="koordinat_a" method="POST" action="">
Latitude : <input type="text" class="form-control" id="lattext" name="lattext" placeholder=" Latitude" style="width:310px"> &nbsp;&nbsp;
Longitude : <input type="text" class="form-control" id="lngtext" name="lngtext" placeholder=" Longitude" style="width:310px"> &nbsp;&nbsp;                
 <button type="button" id ="pilih" name="pilih" class="btn btn-primary" onClick="return select_titik(document.getElementById('lattext').value,document.getElementById('lngtext').value)">Choose</button>
</form>

完美显示在文本框中的单击坐标的纬度和经度。我的问题是只要点击地图就添加标记。 有没有人可以帮助我?你的帮助会很有帮助。谢谢......

2 个答案:

答案 0 :(得分:0)

尝试将lat / lng传递给标记函数,然后实例化另一个LatLng对象。

function add_marker(lat, lng)
        {
                var location = new Google.maps.LatLng(lat, lng);
                var marker = new google.maps.Marker({
                    position : location,
                    title : "Titik A",
                    draggable : false,
                    map : google_map
                });

            }

答案 1 :(得分:0)

您有一个名为add_marker的功能,可以立即使用。调用它并传入您创建的位置对象。

尝试:

<script type="text/javascript">
    var google_map;
    function tampil_peta()
    {
        var div_peta = document.getElementById('kanvas');
        var tengah = new google.maps.LatLng(-8.801502,115.174794);
        var options = 
        {
            center : tengah,
            zoom : 14,
            mapTypeId : google.maps.MapTypeId.ROADMAP
        }

        //membuat objek peta Google Maps
        google_map = new google.maps.Map(div_peta,options);

        google.maps.event.addListener(google_map, "click", function (e) 
        {
            var latitude = e.latLng.lat();
            var longitude = e.latLng.lng();
            var location = new google.maps.LatLng(latitude,longitude);

            document.getElementById('lattext').value = latitude;
            document.getElementById('lngtext').value = longitude;
            add_marker(location);
        });
    }
    function add_marker(location)
    {
        var marker = new google.maps.Marker({
            position : location,
            title : "Titik A",
            draggable : false,
            map : google_map
        });

    }

</script>