我有以下链接:
<a href="#" id="context-map" onclick="showMap(57.8166994,28.33447339999998)">choose on map</a>
showMap
接受地理编码坐标并在此处显示标记。
function showMap(lat,lng) {
window.open('map?lat='+lat+'&lng='+lng, 'map', 'width=600,height=400');
}
在map.jsp上我有以下js:
function initialize() {
var myLatlng = new google.maps.LatLng("${lat}","${lng}");
var mapOptions = {
zoom: 12,
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
marker.setMap(null);
marker = new google.maps.Marker({
position: location,
map: map
});
}
}
我希望在我在地图上更改标记位置后实现这一点,此更改应该影响showMap参数
showMap(changedMarkerLat, changedMarkerLng)
答案 0 :(得分:0)
可能的方法:不是将lat / lng作为参数传递给函数(如@esc所建议的那样),而是将这些数据存储在某处并在函数内部访问它们。
示例:(将lat / lng存储为链接的属性)
<a href="#" id="context-map"
data-lat="57.8166994"
data-lng="28.33447339999998"
onclick="showMap(this)">choose on map</a>
function showMap(o) {
//you only need to open a new window when its not open yet
if(!o.map ||o.map.closed){
o.map = window.open('map?lat='+o.getAttribute('data-lat')+
'&lng='+o.getAttribute('data-lng'),
'map',
'width=600,height=400');
}
//otherwise only bring the window into front
else{
o.map.focus();
}
}
在新窗口中设置链接的data-lat
和data-lng
- 属性
function initialize() {
var myLatlng = new google.maps.LatLng("${lat}","${lng}");
var mapOptions = {
zoom: 12,
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
try{//use a try-catch to avoid errors when the opener has been closed
var link=opener.document.getElementById('context-map');
link.setAttribute('data-lat',location.lat());
link.setAttribute('data-lng',location.lng());
}catch(e){}
//there's no need to create a new Marker,
//simply update the position of the existing Marker
marker.setPosition(location);
}
}
演示:http://www.googledrive.com/host/0BwPgjOA-i5WyQ0VmR3JZTU9BS0U