google地图marker添加div提示信息

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>地图</title>
 <script type="text/javascript" src="http://ditu.google.cn/maps/api/js?sensor=true&language=zh"></script>
   
    <script type="text/javascript">
     var map;
  
        NameOverlay.prototype = new google.maps.OverlayView();
  // NameOverlay定义
  function NameOverlay(point, name, map) {
            // 初始化参数:坐标、文字、地图
   this.point_ = point;
   this.name_ = name;
   this.map_ = map;
  
   
   // 到onAdd时才需要创建div
   this.div_ = null;
  
   // 加入map
   this.setMap(map);
   }
 
        NameOverlay.prototype.onAdd = function() {
 
   // 创建一个div,其中包含了当前文字
   var div = document.createElement('DIV');
   div.style.borderStyle = "none";
   div.style.borderWidth = "0px";
   div.style.position = "absolute";
   //div.style.background = "red";
  
   var span = document.createElement("span");
   var text = document.createTextNode(this.name_);
   span.appendChild(text);
   div.appendChild(span);
  
   // Set the overlay's div_ property to this DIV
   this.div_ = div;
 
   // We add an overlay to a map via one of the map's panes.
   // We'll add this overlay to the overlayImage pane.
   var panes = this.getPanes();
   panes.overlayImage.appendChild(div);
  }
 
  NameOverlay.prototype.draw = function() {
 
   // 利用projection获得当前视图的坐标
   var overlayProjection = this.getProjection();
  
   var center = overlayProjection.fromLatLngToDivPixel(this.point_);
  
   // 为简单,长宽是固定的,实际应该根据文字改变
   var div = this.div_;
   div.style.left = center.x + 'px';
   div.style.top = center.y + 'px';
   div.style.width = '100px';
   div.style.height = '20px';
  }
 
  NameOverlay.prototype.onRemove = function() {
   this.div_.parentNode.removeChild(this.div_);
   this.div_ = null;
  }
  
  function initialize() {
   var mapCenter = new google.maps.LatLng(39.917, 116.397);
   var mapOptions = {
    zoom: 14,
    center: mapCenter,
    mapTypeId: google.maps.MapTypeId.ROADMAP
   }
   map = new google.maps.Map(document.getElementById("map"), mapOptions);
   var str = "北海,39.927, 116.397;北师大,39.938, 116.387";
   RealTimeInfo(str);
  }
  
  /**实时视频
   * 参数("name1,x,y;name2,x,y")
   * name:设备名称,x:纬度,y:经度
   */
  function RealTimeInfo(coordinate) 
  {
      var coordinate = coordinate+'';
   if(coordinate!=''){
    var devArr = coordinate.split(";");
    var len = devArr.length;
    for(var i=0;i<len;i++){
     var dev = devArr[i].split(',');
     var markerLatLng = new google.maps.LatLng(dev[1], dev[2]);
     var marker2 =
     new google.maps.Marker({
      position: markerLatLng,
      map: map,
      title:dev[0]
     });
     new NameOverlay(markerLatLng, dev[0], map);
    }
   }
  }

 </script>
</head>
<body οnlοad="initialize();" style="margin:0;background:#e3e9f7; background-attachment:fixed;">
    <div id="map" style="width:100%; height:100%;"></div>
 
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在高德地图添加 marker 点,可以按照以下步骤进行: 1. 引入高德地图 JavaScript API 库 在 HTML 页面中引入高德地图 JavaScript API 库,可以在官网上下载或使用 CDN 引入。 2. 创建地图 使用 `new AMap.Map()` 方法创建一个地图实例,指定地图容器的 ID 和地图的中心点和缩放级别。 ``` var map = new AMap.Map('mapContainer', { center: [116.397428, 39.90923], zoom: 13 }); ``` 3. 创建 marker 点 使用 `new AMap.Marker()` 方法创建一个 marker 点,指定 marker 点的位置和其他属性。 ``` var marker = new AMap.Marker({ position: [116.397428, 39.90923], title: '这是一个 marker 点', icon: 'https://webapi.amap.com/images/0.png' }); ``` 4. 将 marker添加地图中 使用 `map.add(marker)` 方法将 marker添加地图中。 ``` map.add(marker); ``` 完整的代码示例: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>高德地图添加 marker 点</title> <script src="https://webapi.amap.com/maps?v=1.4.15&key=你的高德地图 API Key"></script> <style type="text/css"> #mapContainer { width: 100%; height: 500px; } </style> </head> <body> <div id="mapContainer"></div> <script type="text/javascript"> var map = new AMap.Map('mapContainer', { center: [116.397428, 39.90923], zoom: 13 }); var marker = new AMap.Marker({ position: [116.397428, 39.90923], title: '这是一个 marker 点', icon: 'https://webapi.amap.com/images/0.png' }); map.add(marker); </script> </body> </html> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值