我正在创建Google地图标记并为其分配ID,如下所示:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.Latitude, data.Longitude),
map: map,
id: data.ID
});
这会导致标记显示在地图上,但是我还想稍后更新标记的位置,如何通过我分配给它的ID来获取标记,然后更新它的位置?
我知道您可以使用Google API中标记对象的setPosition(latlng:LatLng)更新位置,但如何通过ID获取标记呢?
答案 0 :(得分:-1)
试试此代码
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR-API-KEY&libraries=places" type="text/javascript"></script>
<script>
$(document).ready(function () {
var long = $('#ContentPlaceHolder1_hidlog').val();
var lati = $('#ContentPlaceHolder1_hidlat').val();
var mapCenter = new google.maps.LatLng(lati, long);
var map;
map_initialize();
function map_initialize() {
var googleMapOptions =
{
center: mapCenter,
zoom: 18,
panControl: true,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var marker = new google.maps.Marker({
position: mapCenter,
title: "Event Map"
});
map = new google.maps.Map(document.getElementById("google_map"), googleMapOptions);
marker.setMap(map);
}
var getCen = map.getCenter();
google.maps.event.addDomListener(window, 'resize', function () {
map.setCenter(getCen);
});
google.maps.event.addDomListener(window, 'load', map_initialize());
google.maps.event.addDomListener(window, "resize", resizingMap());
$('#showmap').on('show.bs.modal', function () {
resizeMap();
})
function resizeMap() {
if (typeof map == "undefined") return;
setTimeout(function () { resizingMap(); }, 400);
}
function resizingMap() {
if (typeof map == "undefined") return;
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
}
});
</script>