簡體   English   中英

如何通過點擊谷歌地圖添加標記

[英]How to add marker by clicking on google map

我想通過點擊谷歌地圖添加標記。 我使用此鏈接向Google Map V3用戶添加標記

但是這個例子對我沒有幫助。 這是我的代碼:

<script>
        var locations = <?=$json?>;
        function initialize() {
            addMarker(42.862101,74.610684); // add markers on map
        }

        google.maps.event.addDomListener(window, 'load', initialize);
</script>

從數據庫mysql添加標記的功能:

<script>

    function addMarker(x,y){
        var myLatlng = new google.maps.LatLng(x,y);
        var mapOptions = {
            zoom: 8,
            center: myLatlng,
            minZoom: 3
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        var contentString = '<div id="content">'+
            '<div id="siteNotice">'+
            '</div>'+
            '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
            '<div id="bodyContent">'+
            '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
            'Heritage Site.</p>'+
            '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
            'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
            '(last visited June 22, 2009).</p>'+
            '</div>'+
            '</div>';
        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });
        var marker;
        for (var i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i]['coord_lat'], locations[i]['coord_lng']),
                map: map
            });
            //something details
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i]['nameid']);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    }
</script>
# Add marker where user clicks map  
var map = new google.maps.Map(document.getElementById("map"), {
    zoom: 5,
    center: new google.maps.LatLng(40.747688, -74.004142),
    mapTypeId: google.maps.MapTypeId.ROADMAP    
});

google.maps.event.addListener(map, 'click', function(e) {        
    var marker = new google.maps.Marker({
        position: e["latLng"],
        title: "Hello world!"
    });       
    marker.setMap(map);
});        

試試看http://jsfiddle.net/4hf78ag7/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM