/**
* GOOGLE地图开发使用工具
* @author BOONYACHENGDU@
* @date 2013-08-23
* @notice 地图容器的z-index不能小于0,否则鼠标地图无法进行地图操作(可以看到地图,不过你会万分苦恼)
*/
(function(){
={};
=null;
=[];
=null;
={};
GoogleUtil={
CONSTANT:{
mapkey:'AIzaSyAY-HsXXPsBUqsbQLDFO8kpNWLANwH0E7k',
container:"map",
DEFAULT_ZOOM:12,
zoomAddFeature:19,
centerLat:30.65721817,
centerLng:104.06594494,
mapstatus:false,
isnewMap:false,
ZOOM_MAX:19,
ZOOM_MIN:0,
markerSize:32
},
/**
* 控制地图显示范围为中国
*/
mapShowBounds:function(){
var strictBounds = new (
new (14.48003790418668, 66.28120434863283),
new (54.44617552862156, 143.71284497363283)
);
(map, 'dragend',function() {
if ((())) {
return;
}
var c = (),
x = (),
y = (),
maxX = ().lng(),
maxY = ().lat(),
minX = ().lng(),
minY = ().lat();
if (x < minX){x = minX;}
if (x > maxX){x = maxX;}
if (y < minY){y = minY;}
if (y > maxY){y = maxY;}
(new (y, x));
});
},
/**
* 控制地图的缩放级别
*/
limitShowMapZoom:function(zoom){
=zoom;
var limitedZoom=;
(map, 'zoom_changed',function() {
if (() < limitedZoom){(limitedZoom);}
});
},
/**
* 异步加载谷歌API
*/
loadScript:function(){
var script = ("script");
= "text/javascript";
= "/maps/api/js?v=3&key="++"&sensor=false&libraries=drawing,places";
(script);
},
/**
* 谷歌街道
*/
initStreetMap:function(key){
=key|| ;
var mapOptions = {
center: new (,),
zoom: .DEFAULT_ZOOM,
panControl: true,
zoomControl: true,
mapTypeControl: false,
scaleControl: true,
scrollwheel:true,
draggable:true,
overviewMapControl: true,
streetViewControl:true,
mapTypeId: ,
navigationControlOptions: {
style: .ZOOM_PAN,
position: .TOP_LEFT
},
zoomControlOptions:{
style: ,//DEFAULT,LARGE,SMALL
position: .TOP_LEFT
}
};
map = new ((),mapOptions);
//测距控件
();
//清除测距
();
},
/**
* 谷歌卫星
*/
initSatelliteMap:function(key){
=key|| ;
var mapOptions = {
center: new (,),
zoom: .DEFAULT_ZOOM,
panControl: true,
zoomControl: true,
mapTypeControl: false,
scrollwheel:true,
draggable:true,
scaleControl: true,
overviewMapControl: true,
mapTypeId: ,
navigationControlOptions: {
style: .ZOOM_PAN,
position: .TOP_LEFT
},
zoomControlOptions:{
style: ,//DEFAULT,LARGE,SMALL
position: .TOP_LEFT
}
};
map = new ((),mapOptions);
//测距控件
();
//清除测距
();
},
/**
* 谷歌手机
*/
initMobileStreetMap:function(container,key){
=key|| ;
var mapOptions = {
center: new (,),
zoom: .DEFAULT_ZOOM,
panControl: false,
zoomControl: true,
mapTypeControl: false,
scaleControl: true,
scrollwheel:true,
draggable:true,
overviewMapControl: true,
streetViewControl:true,
mapTypeId: ,
navigationControlOptions: {
style: .ZOOM_PAN,
position: .TOP_LEFT
},
zoomControlOptions:{
style: ,//DEFAULT,LARGE,SMALL
position: .TOP_LEFT
}
};
map = new ((container||),mapOptions);
//();
},
/**
* 居中或缩放
*/
centerAndZoom:function(latlng,zoom){
if(latlng){(latlng);}
if(zoom){(zoom);}
},
/**
* 获取图片对象
*/
getIcon:function(imageUrl,size){
var imgSize=size||32;
var offSize=imgSize/2;
var defaultSize=new (imgSize, imgSize);
var myIcon={
url: imageUrl,
size: defaultSize,
scaledSize:new (imgSize,imgSize),
origin: new (0,0),
anchor: new (offSize,offSize)
};
return myIcon;
},
/**
* 居中并缩放
*/
centerAndZoom:function(point,zoom){
(point);
(zoom);
},
/**
* 创建一个地图bounds对象
* @param points
*/
createBounds:function(points){
if(points) {
var bounds=new ();
for ( var i = 0; i < ; i++) {
var point=points[i];
if(point){
(point);
}
}
return bounds;
}
return null;
},
/**
* 设置适合的地图边界范围Bounds
* @param points
*/
panToBounds:function(points){
if(points){
var bounds=(points);
if(bounds){(bounds);}
}
},
/**
* 设置合适的覆盖物显示范围(覆盖物聚合)
*/
getViewport:function(points){
if(points){
var bounds=(points);
if(bounds) {
(bounds);
}
}
},
/**
* 点越界处理
* @param point
*/
ifOutBoundsPantoCenter:function(point){
var bounds=();
var flag=(point);
if(flag==false){
(point);
(point, ());
}
}
};
var iterator=0,scount=0,playStatus=0;
={
/**
* 创建信息窗体
*/
createInfoWindow:function(latlng,htmlContent){
var infowindow = new ({
content: htmlContent,
position:latlng,
disableAutoPan:false
});
return infowindow;
},
/**
* 添加信息窗体
*/
addInfoWindow:function(latlng,htmlContent,isCenter){
if(!infoWindow){
infoWindow=(latlng, htmlContent);
}else{
();
(latlng);
(htmlContent);
}
(map);
if(isCenter){(latlng);}
},
/**
* 创建普通标注
*/
createMarker:function(id,title,point,icon){
var marker = new ({
position: point,
map: map,
icon:icon,
id:id
});
=id;
(title);
return marker;
},
/**
* 创建带有文本描述的标注
*/
createMarkerWithLabel:function(id,title,point,icon){
var marker = new MarkerWithLabel({
position: point,
draggable: false,
map: map,
labelContent: title,
labelAnchor: new (-1, 15),
labelClass: "labels",
labelStyle: {opacity: 1.0,borderColor:'green',color:'navy',fontSize:'12px'},
icon: icon
});
=id;
return marker;
},
/**
* 添加普通标注
*/
addCommonMarker:function(id,title,point,icon){
var marker =(id, title, point, icon);
(marker);
(map);
return marker;
},
/**
* 添加文本标注
*/
addMarkerWithLabel:function(id,title,point,icon){
var marker =(id, title, point, icon);
(marker);
(map);
return marker;
},
/**
* 添加标注
*/
addMarker:function(id,title,point,icon){
var marker =(id, title, point, icon);
return marker;
},
/**
* 批量添加标注
*/
addMarkers:function(points){
if(points){
for ( var i = 0; i < ; i++) {
var point=points[i];
(point);
}
}
},
/**
* 添加跟踪轨迹线条
*/
addLineFeature:function(id,points,style){
lineFeature = new ({
path:points,
strokeWeight : ,
strokeColor :,
map: map
});
=id;
=id;
(lineFeature);
return lineFeature;
},
/**
* 添加折线(轨迹,包括起点、终点)
*/
addLineFeatureAndStartAndEndPoint:function(spObj,points, startImageUrk,endImageUrk,lineStyle){
var len=;
var index =len - 1;
var startPoint = points[0];
var endPoint =points[index];
var startIcon = (startImageUrk,20);
var endIcon = (endImageUrk,20);
("start", , startPoint, startIcon);
("end", , endPoint, endIcon);
if(len>=2){
var d=(len/2)+"";
d=parseInt(d);
(points[d],12);
}
("track_line",points,lineStyle);
},
/**
* 标注动画
*/
markerAnimate:{
dropSetTimeout:{
drop:function(points){
iterator=0;
for (var i = 0; i < ; i++) {
setTimeout(function() {
(points);
}, i * 200);
}
},
addMarker:function(points){
(new ({
position: points[iterator],
map: map,
draggable: false,
animation:
}));
iterator++;
}
}
},
/**
* 轨迹操作
*/
track:{
/**
* 添加轨迹线条
*/
addLineTrack:function(points){
if(points){
var lineCoordinates=[];
for ( var i = 0; i < ; i++) {
var point=points[i];
if(point){
(point);
}
}
var lineSymbol = {
path: .FORWARD_CLOSED_ARROW,
//scale: 2,
strokeColor: 'green'
};
lineFeature = new ({
path: lineCoordinates,
strokeColor: 'red',
strokeWeight : 3,
icons: [{
icon: lineSymbol,
offset: '0%'
}],
map: map
});
="track_line";
}
},
/**
* 轨迹回放操作
*/
operate:{
count:0,
object:null,
addListener:function(){
var animate=(function() {
scount = (scount + 1) % 200;
var icons = ('icons');
icons[0].offset = (scount / 2) + '%';
('icons', icons);
//终点停车
if((scount / 2)>=99){
clearInterval(this);
}
}, 1000);
=animate;
},
play:function(){
playStatus=1;
scount=0;
=playStatus;
();
},
continuePlay:function(){
playStatus=3;
=playStatus;
();
},
pause:function(){
playStatus=2;
=playStatus;
if(){clearInterval();}
},
stop:function(){
playStatus=4;
=playStatus;
if(){clearInterval();}
scount=0;
}
}
},
getOverlayByNodeId:function(id,value){
for (var i = 0; i < ; i++) {
var marker=markers[i];
if(marker[id]==value){
return marker;
}
}
return null;
},
/**
* 删除或显示覆盖物
*/
deleteOrShowMarkerOverlayers:function(map){
for (var i = 0; i < ; i++) {
if(map==null){markers[i].setVisible(false);}
markers[i].setMap(map);
}
if(map==null){markers = [];}
},
/**
* 删除轨迹
*/
deleteTrack:function(){
if(lineFeature){
(false);
(null);
}
},
/**
* 移除所有覆盖物
*/
removeAllOverlays:function(){
for (var i = 0; i < ; i++) {
markers[i].setVisible(false);
markers[i].setMap(map);
}
markers = [];
},
/**
* 移除一个覆盖物
*/
removeOverlay:function(propertyName,value){
if(value){
for (var i = 0; i < ; i++) {
var marker=markers[i];
if(marker[propertyName]==value){
markers[i].setVisible(false);
markers[i].setMap(map);
}
}
}
if(propertyName=="track"||propertyName=="track_line"){
if(lineFeature){
(false);
(null);
lineFeature=null;
}
}
},
/**
* 显示或隐藏标注
*/
isToShowMarkers:function(markers,bool){
if(markers){
for (var i = 0; i < ; i++) {
var marker=markers[i];
(bool);
}
}
},
/**
* 删除轨迹覆盖物
*/
removeTrackLineWithStartAndEndOverlay:function(){
("id", "track_line");
("id", "track");
("id", "start");
("id", "end");
if(lineFeature){
(false);
(null);
lineFeature=null;
}
();
}
};
={
/**
* 地图缩放事件
*/
mapZoomChanged:function(markers,zoom){
var listener=(map, 'zoom_changed', function(event) {
if(()<zoom){
var myMarkers=markers;
(markers,false);//隐藏标注
markers=myMarkers;
}else{
(markers,true);//显示标注
}
});
return listener;
},
/**
* 点击标注事件
*/
markerClick:function(marker){
var listener=(marker, 'click', function(event) {
(map,marker);
});
return listener;
},
/**
* 移除监听对象
*/
removeListener:function(listener){
(listener);
}
};
//测距变量
var polyline;
var polylinesArray = [];
//距离标记数组
var lenArray = [];
var rulerActions=[];
={
/**
* 测距控件
*/
addRulerControl:function(){
var RulerControl=function(rulerControlDiv, map){
= '5px';
// CSS 边框
var controlUI = ('div');
= 'white';
= '#888888';
= 'solid';
= '1px';
= 'pointer';
= 'center';
= '点击测量距离';
(controlUI);
// CSS 文本
var controlText = ('div');
= 'Arial,sans-serif';
= '10px';
= '4px';
= '4px';
= '测距';
(controlText);
(controlUI, 'click', function() {
//启动整个地图的click侦听
var rule = (map,"click",function(event){
var marker = new MarkerWithLabel({
position: ,
draggable: false,
map: map,
labelContent: "0.000公里",
labelAnchor: new (-1, -1),
labelClass: "labels",
labelStyle: {opacity: 1.0},
icon: {}
});
//将标记压入数组
(marker);
//计算距离
var distance = ();
("labelContent",distance);
});
(rule);
});
};
var rulerControlDiv = ('div');
RulerControl(rulerControlDiv, map);
= 1;
[.LEFT_TOP].push(rulerControlDiv);
},
/**
* 清除测距
*/
addClearRulerOverlayControl:function(){
var RulerControl=function(rulerControlDiv, map){
= '5px';
// CSS 边框
var controlUI = ('div');
= 'white';
= '#888888';
= 'solid';
= '1px';
= 'pointer';
= 'center';
= '点击清除测距';
(controlUI);
// CSS 文本
var controlText = ('div');
= 'Arial,sans-serif';
= '10px';
= '4px';
= '4px';
= '清除';
(controlText);
(controlUI, 'click', function() {
while(rulerActions[0]){
(());
}
while(lenArray[0]){
().setMap(null);
}
if (polylinesArray) {
for (i in polylinesArray) {
polylinesArray[i].setMap(null);
}
polylinesArray = [];
}
});
};
var rulerControlDiv = ('div');
RulerControl(rulerControlDiv, map);
= 1;
[.LEFT_TOP].push(rulerControlDiv);
},
/**
* 测距绘制
*/
drawOverlay:function(){
//路线数组
var flightPlanCoordinates = [];
//将坐标压入路线数组
if (lenArray) {
for (i in lenArray) {
(lenArray[i].getPosition());
}
}
//路径选项
var polylineOptions = {
path : flightPlanCoordinates,
map : map,
strokeColor : "#FC7F43",
strokeOpacity : 1.0,
strokeWeight : 2
};
polyline = new (polylineOptions);
//清除原有折线路径
if (polylinesArray) {
for (i in polylinesArray) {
polylinesArray[i].setMap(null);
}
polylinesArray = [];
}
(map);
(polyline);
return ((()/1000).toFixed(3) + "公里");
}
};
//google 测距
= function(latlng) {
var lat = [(), ()];
var lng = [(), ()];
var R = 6378137;
var dLat = (lat[1] - lat[0]) * / 180;
var dLng = (lng[1] - lng[0]) * / 180;
var a = (dLat / 2) * (dLat / 2) + (lat[0] * / 180) * (lat[1] * / 180) * (dLng / 2) * (dLng / 2);
var c = 2 * Math.atan2((a), (1 - a));
var d = R * c;
return (d);
};
= function(marker) {
return ().distanceFrom(());
};
= function() {
var d = 0;
var path = ();
var latlng;
for (var i = 0; i < () - 1; i++) {
latlng = [(i), (i + 1)];
d += latlng[0].distanceFrom(latlng[1]);
}
return d;
};
})();
//window.οnlοad= ();