1. /**
  2. * GOOGLE地图开发使用工具
  3. * @author BOONYACHENGDU@GMAIL.COM
  4. * @date 2013-08-23
  5. * @notice 地图容器的z-index不能小于0,否则鼠标地图无法进行地图操作(可以看到地图,不过你会万分苦恼)
    * @updateLog:在v1.0上添加了地图覆盖物视图显示自适应(核心代码map.fitBounds(bounds);),添加谷歌手机地图,轨迹回放全套测试通过(v1.0轨迹回放不可用)
    */
  6. (function(){
  7. window.map={};
  8. window.lineFeature=null;
  9. window.markers=[];
  10. window.infoWindow=null;
  11. window.GoogleUtil={};
  12. GoogleUtil={
  13. CONSTANT:{
  14. mapkey:'AIzaSyAY-HsXXPsBUqsbQLDFO8kpNWLANwH0E7k',
  15. container:"map",
  16. DEFAULT_ZOOM:12,
  17. zoomAddFeature:19,
  18. centerLat:30.65721817,
  19. centerLng:104.06594494,
  20. mapstatus:false,
  21. isnewMap:false,
  22. ZOOM_MAX:19,
  23. ZOOM_MIN:1,
  24. markerSize:32
  25. },
  26. /**
  27. * 控制地图显示范围为中国
  28. */
  29. mapShowBounds:function(){
  30. var strictBounds = new google.maps.LatLngBounds(
  31. new google.maps.LatLng(14.48003790418668, 66.28120434863283),
  32. new google.maps.LatLng(54.44617552862156, 143.71284497363283)
  33. );
  34. google.maps.event.addListener(map, 'dragend',function() {
  35. if (strictBounds.contains(map.getCenter())) return;
  36. var c = map.getCenter(),
  37. x = c.lng(),
  38. y = c.lat(),
  39. maxX = strictBounds.getNorthEast().lng(),
  40. maxY = strictBounds.getNorthEast().lat(),
  41. minX = strictBounds.getSouthWest().lng(),
  42. minY = strictBounds.getSouthWest().lat();
  43. if (x < minX) x = minX;
  44. if (x > maxX) x = maxX;
  45. if (y < minY) y = minY;
  46. if (y > maxY) y = maxY;
  47. map.setCenter(new google.maps.LatLng(y, x));
  48. });
  49. },
  50. /**
  51. * 控制地图的缩放级别
  52. */
  53. limitShowMapZoom:function(zoom){
  54. this.CONSTANT.zoomMax=zoom;
  55. var limitedZoom=this.CONSTANT.zoomMax;
  56. google.maps.event.addListener(map, 'zoom_changed',function() {
  57. if (map.getZoom() < limitedZoom) map.setZoom(limitedZoom);
  58. });
  59. },
  60. /**
  61. * 异步加载谷歌API
  62. */
  63. loadScript:function(){
  64. var script = document.createElement("script");
  65. script.type = "text/javascript";
  66. script.src = "http://maps.googleapis.com/maps/api/js?v=3&key="+this.CONSTANT.mapkey+"&sensor=false&libraries=drawing,places";
  67. document.body.appendChild(script);
  68. },
  69. /**
  70. * 谷歌街道
  71. */
  72. initStreetMap:function(key){
  73. this.CONSTANT.mapkey=key|| this.CONSTANT.mapkey;
  74. var mapOptions = {
  75. center: new google.maps.LatLng(GoogleUtil.CONSTANT.centerLat,GoogleUtil.CONSTANT.centerLng),
  76. zoom: this.CONSTANT.DEFAULT_ZOOM,
  77. panControl: true,
  78. zoomControl: true,
  79. mapTypeControl: false,
  80. scaleControl: true,
  81. scrollwheel:true,
  82. draggable:true,
  83. overviewMapControl: true,
  84. streetViewControl:true,
  85. mapTypeId: google.maps.MapTypeId.ROADMAP,
  86. navigationControlOptions: {
  87. style: google.maps.NavigationControlStyle.ZOOM_PAN,
  88. position: google.maps.ControlPosition.TOP_LEFT
  89. },
  90. zoomControlOptions:{
  91. style: google.maps.ZoomControlStyle.SMALL,//DEFAULT,LARGE,SMALL
  92. position: google.maps.ControlPosition.TOP_LEFT
  93. }
  94. };
  95. map = new google.maps.Map(document.getElementById(this.CONSTANT.container),mapOptions);
  96. },
  97. /**
  98. * 谷歌卫星
  99. */
  100. initSatelliteMap:function(key){
  101. this.CONSTANT.mapkey=key|| this.CONSTANT.mapkey;
  102. var mapOptions = {
  103. center: new google.maps.LatLng(GoogleUtil.CONSTANT.centerLat,GoogleUtil.CONSTANT.centerLng),
  104. zoom: this.CONSTANT.DEFAULT_ZOOM,
  105. panControl: true,
  106. zoomControl: true,
  107. mapTypeControl: false,
  108. scrollwheel:true,
  109. draggable:true,
  110. scaleControl: true,
  111. overviewMapControl: true,
  112. mapTypeId: google.maps.MapTypeId.SATELLITE,
  113. navigationControlOptions: {
  114. style: google.maps.NavigationControlStyle.ZOOM_PAN,
  115. position: google.maps.ControlPosition.TOP_LEFT
  116. },
  117. zoomControlOptions:{
  118. style: google.maps.ZoomControlStyle.SMALL,//DEFAULT,LARGE,SMALL
  119. position: google.maps.ControlPosition.TOP_LEFT
  120. }
  121. };
  122. map = new google.maps.Map(document.getElementById(this.CONSTANT.container),mapOptions);
  123. },
  124. /**
  125. * 谷歌手机
  126. */
  127. initMobileStreetMap:function(container,key){
  128. this.CONSTANT.mapkey=key|| this.CONSTANT.mapkey;
  129. var mapOptions = {
  130. center: new google.maps.LatLng(GoogleUtil.CONSTANT.centerLat,GoogleUtil.CONSTANT.centerLng),
  131. zoom: this.CONSTANT.DEFAULT_ZOOM,
  132. panControl: false,
  133. zoomControl: true,
  134. mapTypeControl: false,
  135. scaleControl: true,
  136. scrollwheel:true,
  137. draggable:true,
  138. overviewMapControl: true,
  139. streetViewControl:true,
  140. mapTypeId: google.maps.MapTypeId.ROADMAP,
  141. navigationControlOptions: {
  142. style: google.maps.NavigationControlStyle.ZOOM_PAN,
  143. position: google.maps.ControlPosition.TOP_LEFT
  144. },
  145. zoomControlOptions:{
  146. style: google.maps.ZoomControlStyle.SMALL,//DEFAULT,LARGE,SMALL
  147. position: google.maps.ControlPosition.TOP_LEFT
  148. }
  149. };
  150. map = new google.maps.Map(document.getElementById(container||this.CONSTANT.container),mapOptions);
  151. //this.mapShowBounds();
  152. },
  153. /**
  154. * 居中或缩放
  155. */
  156. centerAndZoom:function(latlng,zoom){
  157. if(latlng) map.setCenter(latlng);
  158. if(zoom) map.setZoom(zoom);
  159. },
  160. /**
  161. * 获取图片对象
  162. */
  163. getIcon:function(imageUrl,size){
  164. var imgSize=size||32;
  165. var offSize=imgSize/2;
  166. var defaultSize=new google.maps.Size(imgSize, imgSize);
  167. var myIcon={
  168. url: imageUrl,
  169. size: defaultSize,
  170. scaledSize:new google.maps.Size(imgSize,imgSize),
  171. origin: new google.maps.Point(0,0),
  172. anchor: new google.maps.Point(offSize,offSize)
  173. };
  174. return myIcon;
  175. },
  176. /**
  177. * 创建一个地图bounds对象
  178. * @param points
  179. */
  180. createBounds:function(points){
  181. if(points) {
  182. var bounds=new google.maps.LatLngBounds();
  183. for ( var i = 0; i < points.length; i++) {
  184. var point=points[i];
  185. if(point){
  186. bounds.extend(point);
  187. }
  188. }
  189. return bounds;
  190. }
  191. return null;
  192. },
  193. /**
  194. * 设置适合的地图边界范围Bounds
  195. * @param points
  196. */
  197. panToBounds:function(points){
  198. if(points){
  199. var bounds=this.createBounds(points);
  200. if(bounds) map.panToBounds(bounds);
  201. }
  202. },
  203. /**
  204. * 设置合适的覆盖物显示范围(覆盖物聚合)
  205. */
  206. getViewport:function(points){
  207. if(points){
  208. var bounds=this.createBounds(points);
  209. if(bounds) {
  210. map.fitBounds(bounds);
  211. }
  212. }
  213. }
  214. };
  215.  
  216. var iterator=0,scount=0,playStatus=0;
  217.  
  218. GoogleUtil.tools={
  219. /**
  220. * 创建信息窗体
  221. */
  222. createInfoWindow:function(latlng,htmlContent){
  223. var infowindow = new google.maps.InfoWindow({
  224. content: htmlContent,
  225. position:latlng,
  226. disableAutoPan:false
  227. });
  228. return infowindow;
  229. },
  230. /**
  231. * 添加信息窗体
  232. */
  233. addInfoWindow:function(latlng,htmlContent,isCenter){
  234. if(!infoWindow){
  235. infoWindow=this.createInfoWindow(latlng, htmlContent);
  236. }else{
  237. infoWindow.close();
  238. infoWindow.setPosition(latlng);
  239. infoWindow.setContent(htmlContent);
  240. }
  241. infoWindow.open(map);
  242. if(isCenter) map.setCenter(latlng);
  243. },
  244. /**
  245. * 创建标注
  246. */
  247. createMarker:function(id,title,point,icon){
  248. var marker = new google.maps.Marker({
  249. position: point,
  250. map: map,
  251. icon:icon,
  252. id:id
  253. });
  254. marker.id=id;
  255. marker.setTitle(title);
  256. return marker;
  257. },
  258. /**
  259. * 添加标注
  260. */
  261. addMarker:function(id,title,point,icon){
  262. var marker =this.createMarker(id,title,point,icon);
  263. markers.push(marker);
  264. marker.setMap(map);
  265. return marker;
  266. },
  267. /**
  268. * 批量添加标注
  269. */
  270. addMarkers:function(points){
  271. if(points){
  272. for ( var i = 0; i < points.length; i++) {
  273. var point=points[i];
  274. this.addMarker(point);
  275. }
  276. }
  277. },
  278. /**
  279. * 添加跟踪轨迹线条
  280. */
  281. addLineFeature:function(id,points,style){
  282. var lineSymbol = {
  283. path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
  284. //scale: 2,
  285. strokeColor: 'red'
  286. };
  287. var defaultStyle={
  288. path: points,
  289. icons: [{
  290. icon: lineSymbol,
  291. offset: '0%'
  292. }],
  293. map: map
  294. };
  295. lineFeature = new google.maps.Polyline(style||defaultStyle);
  296. lineFeature.id=id;
  297. lineFeature.track=id;
  298. markers.push(lineFeature);
  299. return lineFeature;
  300. },
  301. /**
  302. * 添加折线(轨迹,包括起点、终点)
  303. */
  304. addLineFeatureAndStartAndEndPoint:function(spObj,points, startImageUrk,endImageUrk,lineStyle){
  305. var len=points.length;
  306. var index =len - 1;
  307. var startPoint = points[0];
  308. var endPoint =points[index];
  309. var startIcon = GoogleUtil.getIcon(startImageUrk,20);
  310. var endIcon = GoogleUtil.getIcon(endImageUrk,20);
  311. this.addMarker("start", spObj.start, startPoint, startIcon);
  312. this.addMarker("end", spObj.end, endPoint, endIcon);
  313. if(len>=2){
  314. var d=(len/2)+"";
  315. d=parseInt(d);
  316. GoogleUtil.centerAndZoom(points[d],12);
  317. }
  318. this.addLineFeature("track_line",points,lineStyle);
  319. },
  320. /**
  321. * 标注动画
  322. */
  323. markerAnimate:{
  324. dropSetTimeout:{
  325. drop:function(points){
  326. iterator=0;
  327. for (var i = 0; i < points.length; i++) {
  328. setTimeout(function() {
  329. GoogleUtil.tools.markerAnimate.dropSetTimeout.addMarker(points);
  330. }, i * 200);
  331. }
  332. },
  333. addMarker:function(points){
  334. markers.push(new google.maps.Marker({
  335. position: points[iterator],
  336. map: map,
  337. draggable: false,
  338. animation: google.maps.Animation.DROP
  339. }));
  340. iterator++;
  341. }
  342. }
  343. },
  344. /**
  345. * 轨迹操作
  346. */
  347. track:{
  348. /**
  349. * 添加轨迹线条
  350. */
  351. addLineTrack:function(points){
  352. if(points){
  353. var lineCoordinates=[];
  354. for ( var i = 0; i < points.length; i++) {
  355. var point=points[i];
  356. if(point){
  357. lineCoordinates.push(point);
  358. }
  359. }
  360. var lineSymbol = {
  361. path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
  362. //scale: 2,
  363. strokeColor: 'green'
  364. };
  365. lineFeature = new google.maps.Polyline({
  366. path: lineCoordinates,
  367. strokeColor: 'red',
  368. strokeWeight : 3,
  369. icons: [{
  370. icon: lineSymbol,
  371. offset: '0%'
  372. }],
  373. map: map
  374. });
  375. lineFeature.id="track_line";
  376. }
  377. },
  378. /**
  379. * 轨迹回放操作
  380. */
  381. operate:{
  382. count:0,
  383. object:null,
  384. addListener:function(){
  385. var animate=window.setInterval(function() {
  386. scount = (scount + 1) % 200;
  387. var icons = lineFeature.get('icons');
  388. icons[0].offset = (scount / 2) + '%';
  389. lineFeature.set('icons', icons);
  390. //终点停车
  391. if((scount / 2)>=99){
  392. clearInterval(this);
  393. }
  394. }, 1000);
  395. this.object=animate;
  396. },
  397. play:function(){
  398. playStatus=1;
  399. scount=0;
  400. lineFeature.playStatus=playStatus;
  401. this.addListener();
  402. },
  403. continuePlay:function(){
  404. playStatus=3;
  405. lineFeature.playStatus=playStatus;
  406. this.addListener();
  407. },
  408. pause:function(){
  409. playStatus=2;
  410. lineFeature.playStatus=playStatus;
  411. if(this.object)clearInterval(this.object);
  412. },
  413. stop:function(){
  414. playStatus=4;
  415. lineFeature.playStatus=playStatus;
  416. if(this.object)clearInterval(this.object);
  417. scount=0;
  418. }
  419. }
  420. },
  421. getOverlayByNodeId:function(id,value){
  422. for (var i = 0; i < markers.length; i++) {
  423. var marker=markers[i];
  424. if(marker[id]==value){
  425. return marker;
  426. }
  427. }
  428. return null;
  429. },
  430. /**
  431. * 删除或显示覆盖物
  432. */
  433. deleteOrShowMarkerOverlayers:function(map){
  434. for (var i = 0; i < markers.length; i++) {
  435. if(map==null) markers[i].setVisible(false);
  436. markers[i].setMap(map);
  437. }
  438. if(map==null) markers = [];
  439. },
  440. /**
  441. * 删除轨迹
  442. */
  443. deleteTrack:function(){
  444. if(lineFeature){
  445. lineFeature.setVisible(false);
  446. lineFeature.setMap(null);
  447. }
  448. },
  449. /**
  450. * 移除所有覆盖物
  451. */
  452. removeAllOverlays:function(){
  453. for (var i = 0; i < markers.length; i++) {
  454. markers[i].setVisible(false);
  455. markers[i].setMap(map);
  456. }
  457. markers = [];
  458. },
  459. /**
  460. * 移除一个覆盖物
  461. */
  462. removeOverlay:function(propertyName,value){
  463. if(value){
  464. for (var i = 0; i < markers.length; i++) {
  465. var marker=markers[i];
  466. if(marker[propertyName]==value){
  467. markers[i].setVisible(false);
  468. markers[i].setMap(map);
  469. }
  470. }
  471. }
  472. if(propertyName=="track"||propertyName=="track_line"){
  473. if(lineFeature){
  474. lineFeature.setVisible(false);
  475. lineFeature.setMap(null);
  476. lineFeature=null;
  477. }
  478. }
  479. },
  480. /**
  481. * 显示或隐藏标注
  482. */
  483. isToShowMarkers:function(markers,bool){
  484. if(markers){
  485. for (var i = 0; i < markers.length; i++) {
  486. var marker=markers[i];
  487. marker.setVisible(bool);
  488. }
  489. }
  490. },
  491. /**
  492. * 删除轨迹覆盖物
  493. */
  494. removeTrackLineWithStartAndEndOverlay:function(){
  495. this.removeOverlay("id", "track_line");
  496. this.removeOverlay("id", "track");
  497. this.removeOverlay("id", "start");
  498. this.removeOverlay("id", "end");
  499. if(lineFeature){
  500. lineFeature.setVisible(false);
  501. lineFeature.setMap(null);
  502. lineFeature=null;
  503. }
  504. this.removeAllOverlays();
  505. }
  506. };
  507.  
  508. GoogleUtil.event={
  509. /**
  510. * 地图缩放事件
  511. */
  512. mapZoomChanged:function(markers,zoom){
  513. var listener=google.maps.event.addListener(map, 'zoom_changed', function(event) {
  514. if(map.getZoom()<zoom){
  515. var myMarkers=markers;
  516. GoogleUtil.tools.isToShowMarkers(markers,false);//隐藏标注
  517. markers=myMarkers;
  518. }else{
  519. GoogleUtil.tools.isToShowMarkers(markers,true);//显示标注
  520. }
  521. });
  522. return listener;
  523. },
  524. /**
  525. * 点击标注事件
  526. */
  527. markerClick:function(marker){
  528. var listener=google.maps.event.addListener(marker, 'click', function(event) {
  529. marker.infoWindow.open(map,marker);
  530. });
  531. return listener;
  532. },
  533. /**
  534. * 移除监听对象
  535. */
  536. removeListener:function(listener){
  537. google.maps.event.removeListener(listener);
  538. }
  539. };
  540.  
  541. })();
  542. //window.onload= GoogleUtil.loadScript();

Google map v3 using simple tool file google.map.util.js v 1.1的更多相关文章

  1. Google map v3 using simple tool file google.map.util.js v 1.0

    /** * GOOGLE地图开发使用工具 * @author BOONYACHENGDU@GMAIL.COM * @date 2013-08-23 * @notice 地图容器的(div)z-inde ...

  2. Google map v3 using simple tool file google.map.util.js v 1.2

    更新添加日志:在1.1的基础上添加marker的文字显示.测距工具. /** * GOOGLE地图开发使用工具 * @author BOONYACHENGDU@GMAIL.COM * @date 20 ...

  3. 使用google map v3 api 开发地图服务

    Google Map V3 API 学习地址: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/article ...

  4. Google Maps V3 之 路线服务

    概述 您可以使用 DirectionsService 对象计算路线(使用各种交通方式).此对象与 Google Maps API 路线服务进行通信,该服务会接收路线请求并返回计算的结果.您可以自行处理 ...

  5. Google API v3 设置Icon问题处理

    1.查看API实现 //虽然比较符合API实现的思想但这个没法; //会产生Uncaught TypeError: undefined is not a function //google API n ...

  6. 怎样用Google APIs和Google的应用系统进行集成(2)----Google APIs的全部的RESTFul服务一览

    上篇文章,我提到了,Google APIs暴露了86种不同种类和版本号的API.我们能够通过在浏览器里面输入https://www.googleapis.com/discovery/v1/apis这个 ...

  7. 高效率使用google,国外搜索引擎,国内顺利使用Google的另类技巧,可用谷歌镜像, 可用google学术, 如何使用robots不让百度和google收录

    Google良好的搜索和易用性已经得到了广大网友的欢迎,但是除了我们经常使用的Google网站.图像和新闻搜索之外,它还有很多其他搜索功能和搜索技巧.如果我们也能充分利用,必将带来更大的便利.这里我介 ...

  8. [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具

    转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...

  9. Idea运行时Scala报错Exception in thread "main" java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

    一.情况描述 使用idea +scala+spark,运行程序代码如下: package cn.idcast.hello import org.apache.spark.rdd.RDD import ...

随机推荐

  1. Match类

    Regex在匹配的时候,常常会返回一个Match类的对象,今天就来看看怎么玩这个对象. 一.属性 Captures 按从里到外.从左到右的顺序获取由捕获组匹配的所有捕获的集合(如果正则表达式用 Reg ...

  2. 【DP问题集】动态规划试题

    1.背包问题 给定n种物品和一背包.物品i的重量是wi,其价值为pi,背包的容量为C.问应如何选择装入背包的物品,使得装入背包中物品的总价值最大? 分析: ①每个物品只有两种选择,要么就是塞到包里面, ...

  3. hdu 2202 最大三角形_凸包模板

    题意:略 思路:直接套用凸包模板 #include <iostream> #include <cstdio> #include <cmath> #include & ...

  4. fourinone分布式缓存研究和Redis分布式缓存研究

    最近在写一个天气数据推送的项目,准备用缓存来存储数据.下面分别介绍一下fourinone分布式缓存和Redis分布式缓存,然后对二者进行对比,以供大家参考. 1  fourinone分布式缓存特性 1 ...

  5. JS封装cookie操作函数实例(设置、读取、删除)

    本文实例讲述了JS封装cookie操作函数.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  6. Js apply 方法 具体解释

    Js apply方法具体解释 我在一開始看到javascript的函数apply和call时,很的模糊,看也看不懂,近期在网上看到一些文章对apply方法和call的一些演示样例,总算是看的有点眉目了 ...

  7. linux光盘、U盘的挂载与卸载

    mount [-t vfstype] [-o options] device dir1.-t vfstype 指定文件系统的类型,通常不必指定.mount 会自动选择正确的类型.关于一些常用的文件:i ...

  8. 【Java基础】构造方法调用构造方法

    从一个程序开始: class dog { private String name; private String color; private int age; dog(String name) // ...

  9. 【TFS】增加组员,以及用户权限分配

    一.创建windows用户. 二.TFS ->组成员资格->双击 项目集合管理员->添加创建的Windows用户(最高权限) 三.设置权限: TFS权限的复杂,其实也不是很复杂,它只 ...

  10. url 的httppost 和http get ,put,delect

    URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源 ,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查 ,改 ,增 ,删 4个操作 .到这里 ...