飞道的博客

高德/腾讯地图API实现自定义标记点和事件

451人阅读  评论(0)

主要是基于JavaScript的地图API实现自定义的marker和点击事件,前后端不分离场景,是ajax得到请求;


  
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset= "utf-8">
  5. <meta http-equiv= "X-UA-Compatible" content= "IE=edge">
  6. <meta name= "viewport" content= "initial-scale=1.0, user-scalable=no, width=device-width">
  7. <title>默认点标记</title>
  8. <link rel= "stylesheet" href= "https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
  9. <style>
  10. html, body, #container {
  11. height: 100%;
  12. width: 100%;
  13. }
  14. .amap-icon img,
  15. .amap-marker-content img{
  16. width: 25px;
  17. height: 34px;
  18. }
  19. .marker {
  20. position: absolute;
  21. top: -20px;
  22. right: -118px;
  23. color: #fff;
  24. padding: 4px 10px;
  25. box-shadow: 1px 1px 1px rgba( 10, 10, 10, .2);
  26. white-space: nowrap;
  27. font-size: 12px;
  28. font-family: "";
  29. background-color: #25A5F7;
  30. border-radius: 3px;
  31. }
  32. .input-card{
  33. width: 18rem;
  34. z-index: 170;
  35. }
  36. .input-card .btn{
  37. margin-right: .8rem;
  38. }
  39. .input-card .btn:last-child{
  40. margin-right: 0;
  41. }
  42. </style>
  43. <% include( "/web/include/head.html"){} %>
  44. <script type= "text/javascript"
  45. src= "https://webapi.amap.com/maps?v=1.4.15&key=9ac90d41d649732b840fb59de296d287"></script>
  46. <script type= "text/javascript">
  47. var marker, map = null;
  48. $(function(){
  49. //初始化一个腾讯地图
  50. mapInit();
  51. //alert( "1111----");
  52. getCompanyPosition();
  53. //alert( "4444----");
  54. });
  55. function mapInit() {
  56. //alert( "2222----");
  57. marker, map = new AMap.Map( "container", {
  58. resizeEnable: true,
  59. center: [ 113.5403600000, 34.8043600000],
  60. zoom: 4
  61. });
  62. }
  63. var companyLogoArray=[];
  64. var imgUrl= "${ctxPath}";
  65. function getCompanyPosition() {
  66. // 通过ajax调用后台获取企业信息json
  67. $.ajax({
  68. url: "${ctxPath}/KpCompany/getBaseCompany",
  69. success:function(data){
  70. companyInfoJsonArray = eval(data);
  71. //使用ajax得到的json数组样例
  72. //[{ "logoName": "putty", "companyId": "d139a08326874965be63106a2c10ee6a", "logoSrc": "/KpCompany/showPicture?pk=d139a08326874965be63106a2c10ee6a", "Lng": "1", "Lat": "1", "logoSuffix": ".exe"}]
  73. //addMarkerOne();
  74. //addMarkerTwo();
  75. //marker.on( 'click', showInfoM);
  76. for(var i= 0; i<companyInfoJsonArray.length; i++) {
  77. //得到经纬度地址
  78. var companyName=companyInfoJsonArray[i][ "companyName"];
  79. var longitude=companyInfoJsonArray[i][ "longitude"];
  80. var latitude=companyInfoJsonArray[i][ "latitude"];
  81. //调用经纬度解析方法
  82. var positionArray=[];
  83. positionArray.push(longitude);
  84. positionArray.push(latitude);
  85. //alert(companyName);
  86. //alert(positionArray);
  87. //得到企业的logo的src
  88. var companyLogoSrc=imgUrl+companyInfoJsonArray[i][ "logoSrc"];
  89. //alert(companyLogoSrc);
  90. //得到企业logo的src数组
  91. //companyLogoArray.push(companyLogoSrc);
  92. var markerDesign = new AMap.Marker({
  93. icon: companyLogoSrc,
  94. position: positionArray,
  95. offset: new AMap.Pixel( -13, -30)
  96. });
  97. markerDesign.setMap(map);
  98. //markerDesign.on( 'click', showInfoM);
  99. }
  100. // function addMarkerOne() {
  101. // marker = new AMap.Marker({
  102. // icon: "https://3gimg.qq.com/lightmap/api_v2/2/4/122/theme/default/imgs/marker.png",
  103. // position: [ 121.659637, 42.017033],
  104. // offset: new AMap.Pixel( -13, -30)
  105. // });
  106. // marker.setMap(map);
  107. //
  108. // }
  109. //
  110. //
  111. // function showInfoM(e){
  112. // var text = '您好!'
  113. // document.querySelector( "#text").innerText = text;
  114. // }
  115. //
  116. //
  117. // function addMarkerTwo() {
  118. // marker = new AMap.Marker({
  119. // icon: "https://3gimg.qq.com/lightmap/api_v2/2/4/122/theme/default/imgs/marker.png",
  120. // position: [ 113.5403600000, 34.8043600000],
  121. // offset: new AMap.Pixel( -13, -30)
  122. // });
  123. // marker.setMap(map);
  124. // }
  125. }});
  126. }
  127. // function showInfoM(e){
  128. // js.alert( '中原云数据',{icon: 1})
  129. // }
  130. </script>
  131. </head>
  132. <body>
  133. <div style= "width:1403px;height:600px" id= "container"></div>
  134. <div class="info" id="text">
  135. 请点击覆盖物试试
  136. </div>
  137. </body>
  138. </html>

 腾讯地图API:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
  5. <meta name= "viewport" content= "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  6. <title>demo</title>
  7. <style type= "text/css">
  8. * {
  9. margin: 0px;
  10. padding: 0px;
  11. }
  12. body,
  13. button,
  14. input,
  15. select,
  16. textarea {
  17. font: 12px/ 16px Verdana, Helvetica, Arial, sans-serif;
  18. }
  19. p {
  20. width: 603px;
  21. padding-top: 3px;
  22. margin-top: 10px;
  23. overflow: hidden;
  24. }
  25. input #address {
  26. width: 300px;
  27. }
  28. </style>
  29. <% include( "/web/include/head.html"){} %>
  30. <script charset= "utf-8" src= "https://map.qq.com/api/js?v=2.exp&key=NAEBZ-OVLKU-ELLVG-2Q2WU-DC4AE-VKBGM"></script>
  31. <!--<script type= "text/javascript" src= "jquery-1.9.1.js"></script>-->
  32. <script type= "text/javascript">
  33. //var companyInfoJson=[];
  34. //var companyLocations=[];
  35. var markerObject=[];
  36. var geocoder, map = null;
  37. var companyInfoJsonArray=[];
  38. //var companyInfoJsonArray=[{logoName: "3b57b176939f5475c0e2dd24e24734c", companyId: "625d0db73aa6445c9bcd15c27349e29e", logoSrc: "/KpCompany/showPicture?pk=625d0db73aa6445c9bcd15c27349e29e", companyName: "阿里巴巴", registrationAddr: "杭州市余杭区文一西路969号"}];
  39. var imgUrl= "${ctxPath}/KpCompany/getBaseCompany";
  40. var Map = qq.maps.Map;
  41. var Marker = qq.maps.Marker;
  42. var LatLng = qq.maps.LatLng;
  43. var Event = qq.maps.event;
  44. var MarkerImage = qq.maps.MarkerImage;
  45. var MarkerShape = qq.maps.MarkerShape;
  46. var MarkerAnimation = qq.maps.MarkerAnimation;
  47. var Point = qq.maps.Point;
  48. var Size = qq.maps.Size;
  49. var ALIGN = qq.maps.ALIGN;
  50. var MVCArray = qq.maps.MVCArray;
  51. var MarkerCluster = qq.maps.MarkerCluster;
  52. var markerClusterer = qq.maps.MarkerCluster;
  53. var Cluster = qq.maps.Cluster;
  54. var MarkerDecoration = qq.maps.MarkerDecoration;
  55. var markers = new MVCArray();
  56. //var markerCluster;
  57. //$(function(){
  58. //初始化一个腾讯地图
  59. //mapInit();
  60. //getCompanyPosition();
  61. //getCompanyPosition();
  62. //使用ajax得到的json数组样例
  63. //[{ "logoName": "putty", "companyId": "d139a08326874965be63106a2c10ee6a", "logoSrc": "/KpCompany/showPicture?pk=d139a08326874965be63106a2c10ee6a", "companyName": "1", "registrationAddr": "1", "logoSuffix": ".exe"}]
  64. //logo拼接得到:imgUrl + logoSrc + logoSuffix
  65. //});
  66. //function mapInit() {
  67. //var center = new qq.maps.LatLng( 39.916527, 116.397128);
  68. //map = new qq.maps.Map(document.getElementById( 'container'), {
  69. // center: center,
  70. // zoom: 4,
  71. //});
  72. //}
  73. //使用ajax得到的json数组样例
  74. //[{ "logoName": "putty", "companyId": "d139a08326874965be63106a2c10ee6a", "logoSrc": "/KpCompany/showPicture?pk=d139a08326874965be63106a2c10ee6a", "companyName": "1", "registrationAddr": "1", "logoSuffix": ".exe"}]
  75. // 通过ajax调用后台获取企业信息json
  76. $.ajax({
  77. url: "${ctxPath}/KpCompany/getBaseCompany",
  78. success:function(data){
  79. companyInfoJsonArray = eval(data);
  80. //alert( "1111----"+companyInfoJsonArray.length);
  81. var latlng = new LatLng( 39.91, 116.38);
  82. var options = {
  83. 'zoom': 4,
  84. 'center':latlng,
  85. 'mapTypeId': "roadmap"
  86. };
  87. var map = new Map(document.getElementById( 'map_canvas'), options);
  88. //alert( "map-ok----");
  89. //添加到提示窗
  90. var infoWin = new qq.maps.InfoWindow({
  91. map: map
  92. });
  93. function createCluster() {
  94. for (var i = 0; i < companyInfoJsonArray.length; i++) {
  95. (function(n){
  96. var latLng = new LatLng(companyInfoJsonArray[i][ "latitude"], companyInfoJsonArray[i][ "longitude"]);
  97. var decoration = new MarkerDecoration(i, new Point( 0, -5));
  98. var companyLogoSrc=companyInfoJsonArray[i][ "logoSrc"];
  99. var anchor = new qq.maps.Point( 0, 10),
  100. scaleSize = new qq.maps.Size( 22, 22),
  101. origin = new qq.maps.Point( 0, 0),
  102. icon = new qq.maps.MarkerImage(
  103. companyLogoSrc,
  104. null,
  105. origin,
  106. anchor,
  107. scaleSize
  108. );
  109. var marker = new Marker({
  110. 'position':latLng,
  111. map:map
  112. });
  113. marker.setIcon(icon);
  114. //获取标记的点击事件
  115. var companyName=companyInfoJsonArray[i][ "companyName"];
  116. var registrationAddr=companyInfoJsonArray[i][ "registrationAddr"];
  117. var legalPerson=companyInfoJsonArray[i][ "legalPerson"];
  118. var registrationArea=companyInfoJsonArray[i][ "registrationArea"];
  119. var message= '<div style="text-align:center;"><b>企业信息</b></div>'
  120. + '<div>企业名称:'+companyName+ '</div>'
  121. + '<div>企业法人:'+legalPerson+ '</div>'
  122. + '<div>企业地址:'+registrationAddr+ '</div>'
  123. + '<div>注册地区:'+registrationArea+ '</div>';
  124. qq.maps.event.addListener(marker, 'click', function() {
  125. location.href= '${ctxPath}/KpCompany/companyAudit?op=look&companyId='+companyInfoJsonArray[n][ "companyId"];
  126. });
  127. qq.maps.event.addListener(marker, 'mouseover', function() {
  128. infoWin.open();
  129. infoWin.setContent(message);
  130. infoWin.setPosition(latLng);
  131. });
  132. markerObject.push(companyInfoJsonArray[n]);
  133. markers.push(marker);
  134. })(i);
  135. }
  136. markerClusterer = new MarkerCluster({
  137. map:map,
  138. minimumClusterSize: 2, //默认 2
  139. markers:markers,
  140. zoomOnClick:true, //默认为true
  141. gridSize: 30, //默认 60
  142. averageCenter:true, //默认false
  143. maxZoom: 4, //默认 18
  144. });
  145. Event.addListener(markerClusterer, 'clusterclick', function (evt) {
  146. // writeLog( "mouse event", eventType);
  147. var ss = evt;
  148. //alert( '点击了聚合点');
  149. });
  150. };
  151. createCluster();
  152. }});
  153. //}
  154. function clearOverlays() {
  155. if (markers) {
  156. $.each(markers.elems,function(key ,val){
  157. //清除覆盖层
  158. val.setVisible(false);
  159. if(markerObject[key][ "companyName"].indexOf($( "#keyword").val()) >= 0)
  160. val.setVisible(true);
  161. })
  162. }
  163. }
  164. /*
  165. * set markers
  166. */
  167. </script>
  168. </head>
  169. <body>
  170. <!--<div>
  171. <input id= "address" type= "textbox" value= "中国,北京,海淀区,海淀大街38号">
  172. <button onClick= "codeAddress()">search</button>
  173. </div>
  174. -->
  175. <div>
  176. <input id= "keyword" type= "textbox" value= "中原云数据">
  177. <input type= "button" value= "搜索" onclick= "clearOverlays()">
  178. </div>
  179. <div style= "width:1403px;height:600px" id= "map_canvas"></div>
  180. <!--<p>输入地址,点击search进行地址解释,点击Marker会弹出反查结果。</p>
  181. -->
  182. </body>
  183. </html>

 

 

 


转载:https://blog.csdn.net/yezonggang/article/details/113059049
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场