gpt4 book ai didi

jquery - Google map 标记不在中心

转载 作者:行者123 更新时间:2023-12-01 00:40:33 27 4
gpt4 key购买 nike

我正在实现 Google Map API,并在 jQuery 对话框中显示 map 。 map 显示正常,但标记位置不在中心,所以请指导我。

如何使标记位置和 map 居中?

map 代码:

var myCenter = new google.maps.LatLng('@Model.Latitude', '@Model.Longitude'); 

function initialize() {
var mapProp = {
center: myCenter,
zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker = new google.maps.Marker({
position: myCenter,
draggable: false,
animation: google.maps.Animation.DROP,
});

marker.setMap(map);
}

打开对话框的代码:

$("#googleMap").dialog({ 
autoOpen: false,
height: 600,
width: 1000,
resizeStop: function (event, ui) {
google.maps.event.trigger(googleMap, 'resize')
}, open: function (event, ui) {
google.maps.event.trigger(googleMap, 'resize'); },
});

$("#btnLocation").click(function () {
$("#googleMap").dialog("open");
});

HTML:

<div id="googleMap" style="width: 1000px; height: 500px"></div> 
<img src="map.jpg" alt="Set Location" id="btnLocation" style="cursor: pointer; height: 35px; width: 35px;" />

最佳答案

尝试以下操作:

1) 定义 mapmarker函数外 initialize ,我们稍后将调用它们来使 map 居中

var myCenter = new google.maps.LatLng('@Model.Latitude', '@Model.Longitude');

// DEFINE YOUR MAP AND MARKER
var map = null, marker = null;

function initialize() {
var mapProp = {
center: myCenter,
zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
marker = new google.maps.Marker({
position: myCenter,
draggable: false,
animation: google.maps.Animation.DROP,
});

marker.setMap(map);
}

2)修改对话框的“打开”事件,打开对话框时强制显示 map 中心:

$("#googleMap").dialog({ 
autoOpen: false,
height: 600,
width: 1000,
resizeStop: function (event, ui) {
google.maps.event.trigger(googleMap, 'resize')
},
// OPEN EVENT MODIFIED !
open: function (event, ui) {
google.maps.event.trigger(googleMap, 'resize');

// Force the center of the map
map.setCenter(marker.getPosition());
},
});

尝试一下,让我知道这是否有效;)

关于jquery - Google map 标记不在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21822705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com