简体   繁体   English

Google Maps API:在当前地图位置放置标记

[英]Google Maps API: Place marker at current map location

How would I go about placing a marker at the current centre of the map using a button click? 如何通过单击按钮将标记放置在地图的当前中心?

Currently I have it so a marker is placed at a specific latitude and longitude (53.41291, -8.243889999999965). 目前,我已将标记放置在特定的经度和纬度上(53.41291,-8.243889999999965)。 But I would like it to be placed at the current centre of the map. 但我希望将其放置在地图的当前中心。

Here's my Javascript as it is: 这是我的Javascript:

var latlng = new google.maps.LatLng(53.41291, -8.243889999999965) ;

var infowindow = new google.maps.InfoWindow({
  content: 'This is a place'
  });

function addmarker(latilongi) {
    var marker = new google.maps.Marker({
        position: latilongi,
        title: 'new marker',
        draggable: true,
        map: map
    });
    map.setCenter(marker.getPosition())
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
  });
}


$('#addPoint').on('click', function() {
    addmarker(latlng)
})

You want to centre your marker on the map's centre? 您想将标记放在地图中心吗?

function addmarker() {
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        title: 'new marker',
        draggable: true,
        map: map
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
  });
}

If you also want to be able to click on the map, and add a marker (and presumably then following the same rule, centre the map on that position)... 如果您还希望能够单击地图并添加标记(大概然后遵循相同的规则,则将地图居中于该位置)...

google.maps.event.addListener(map, 'click', function(event) {
    map.setCenter(event.latLng);
    addmarker();
});

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

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