允许用户在Google地图自定义地图中移回标记位置(z-index?) [英] allow user to move marker position back (z-index?) in google maps custom map
本文介绍了允许用户在Google地图自定义地图中移回标记位置(z-index?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望允许用户在InfoWindow内的点击上移动自定义Google地图标记的顺序。这是为了克服重叠标记的问题。我考虑过其他解决方案(移动纬度/经度,标记簇,蜘蛛标记)。
我的代码有两个问题:1)jquery listener不工作2)但更重要的是如何实现z-index(或其他技术?)的更改并重新显示。
<!DOCTYPE html>
< html>
< head>
< style>
#map-canvas,#side-bar {
height:500px;
width:600px;
}
< / style>
< script src =http://maps.googleapis.com/maps/api/jstype =text / javascript>< / script>
< script src =../ jquery / jquery.jstype =text / javascript>< / script>
< script type =text / javascript>
// jQuery的范围
$(document).ready(function(){
use strict;
//变量保存地图
var map;
//变量保存当前活动InfoWindow
var activeInfoWindow;
// b -------------------------------------------------- ----------------------------- //
//初始化函数
// ----- -------------------------------------------------- ------------------------ //
函数initialize(){
// ----- -------------------------------------------------- ------------------------ //
// LISTENER点击事件
// -------- -------------------------------------------------- --------------------- //
$(a).on(click,function(){
alert( 到了这里!);
//做一些事情来改变这个市场的z-index r
// ...
// my_index = my_index-1;
// ...
返回false;
});
地图选项 - 这里提供了很多选项
var mapOptions = {
zoom:5,
draggable:true,
center:new google .maps.LatLng(44.960,-93.100),
mapTypeId:google.maps.MapTypeId.ROADMAP
};
//使用上面定义的地图选项调用map-canvas在div中创建地图
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
//定义两个表示地理点的Google Map LatLng对象
var stPaul = new google.maps.LatLng(44.95283,-93.08925);
var minneapolis = new google.maps.LatLng(44.97984,-93.26620);
//放置两个标记
fnPlaceMarkers(stPaul,St Paul);
fnPlaceMarkers(明尼阿波利斯,明尼阿波利斯);
}
// ------------------------------- ------------------------------------------------ //
//在地图上创建标记
// --------------------------------- ---------------------------------------------- //
函数fnPlaceMarkers(myLocation,myCityName){
var marker = new google.maps.Marker({
position:myLocation
});
//渲染指定地图上的标记
marker.setMap(map);
//创建一个InfoWindow
var infoWnd = new google.maps.InfoWindow();
//将内容添加到InfoWindow
infoWnd.setContent('< div class =scrollFix>'+'欢迎使用'+ myCityName +'< br /> < a href =#>点击< / a>将此标记移动到后面< / div>);
//在InfoWindow上添加监听器 - 在打开新监视器之前关闭最后一个infoWindow
google.maps.event.addListener(marker,'click',function(){
//关闭活动窗口如果存在 - [可能预期这是默认行为no]
if(activeInfoWindow!= null)activeInfoWindow.close();
// Open InfoWindow
infoWnd.open(map,marker);
//将新的InfoWindow存储到全局变量
activeInfoWindow = infoWnd;
});
}
// ------------------------------- ------------------------------------------------ //
//初始加载
// ------------------------------------ ------------------------------------------- //
google .maps.event.addDomListener(window,'load',initialize);
}); //结束查询
< / script>
< div id =map-canvas>< / div>
< / body>
< / html>
解决方案
-
set所有标记的zIndex(否则它没有定义),好值是
(latitude * -100000)<<< 5
(来自迈克尔威廉姆斯的古代历史)var marker = new google.maps.Marker {
position:myLocation,
zIndex:Math.round(myLocation.lat()* - 100000)<5
});
-
保留所有标记的引用(array
markers
pre $markers.push(marker);
当点击链接时, -
将zIndex减少-100000。
查看全文