第4行: | 第4行: | ||
目前JSAPI GL版支持的覆盖物以基本图形为主。<span style="color: rgb(51, 51, 51);">本节重点介绍一下如何向地图添加标注,以及与地图相关的一些交互。</span> | 目前JSAPI GL版支持的覆盖物以基本图形为主。<span style="color: rgb(51, 51, 51);">本节重点介绍一下如何向地图添加标注,以及与地图相关的一些交互。</span> | ||
− | </div><!--jsapi单独demo-代码--><div class="aloneDemo"><div dir="https://lbs.baidu.com/jsdemo/demo/webgl1_0.htm"></div><div class="top_right">[ | + | </div><!--jsapi单独demo-代码--><div class="aloneDemo"><div dir="https://lbs.baidu.com/jsdemo/demo/webgl1_0.htm"></div><div class="top_right">[http://lbs.baidu.com/jsdemo.htm#webgl1_0 DEMO详情]</div></div><div class="bluetitle"><div class="serve-explain-text"><div class="service-page-anchor"><span>提供的覆盖物</span></div></div></div><div class="serve-explain-text"> |
可以使用map.addOverlay方法向地图添加覆盖物,使用map.removeOverlay方法移除覆盖物。 | 可以使用map.addOverlay方法向地图添加覆盖物,使用map.removeOverlay方法移除覆盖物。 | ||
</div> | </div> | ||
第66行: | 第66行: | ||
}); | }); | ||
</pre> | </pre> | ||
− | </div></div><div class="bluetitle"><div class="serve-explain-text"><div class="service-page-anchor"><span>添加折线</span></div></div></div><div class="serve-explain-text">Polyline表示地图上的折线覆盖物,它包含一组点,并将这些点连接起来形成折线。Polyline使用详情请见[http://lbsyun.baidu.com/cms/jsapi/reference/jsapi_webgl_1_0.html 类参考]</div><div class="devguide"><div class="leftborderbg" style="height:5000px;"></div><div class="devguideorder"><span>1</span>添加折线</div><div class="devguidecenter">首先参考展示地图创建基本地图,之后添加折线覆盖物到地图中。折线覆盖物可以分别设置描边粗细、颜色、填充颜色等属性。<pre class="prettyprint codestyle">var polyline = new BMapGL.Polyline([ | + | </div></div><div class="bluetitle"><div class="serve-explain-text"><div class="service-page-anchor"><span>添加折线</span></div></div></div><div class="serve-explain-text">Polyline表示地图上的折线覆盖物,它包含一组点,并将这些点连接起来形成折线。Polyline使用详情请见[http://lbsyun.baidu.com/cms/jsapi/reference/jsapi_webgl_1_0.html 类参考]</div><div class="devguide"><div class="leftborderbg" style="height:5000px;"></div><div class="devguideorder" ><span>1</span>添加折线</div><div class="devguidecenter" style="width: 680px;">首先参考展示地图创建基本地图,之后添加折线覆盖物到地图中。折线覆盖物可以分别设置描边粗细、颜色、填充颜色等属性。<pre class="prettyprint codestyle">var polyline = new BMapGL.Polyline([ |
new BMapGL.Point(116.399, 39.910), | new BMapGL.Point(116.399, 39.910), | ||
new BMapGL.Point(116.405, 39.920), | new BMapGL.Point(116.405, 39.920), | ||
第73行: | 第73行: | ||
map.addOverlay(polyline); | map.addOverlay(polyline); | ||
</pre> | </pre> | ||
− | </div></div><div class="bluetitle"><div class="serve-explain-text"><div class="service-page-anchor"><span>添加多边形</span></div></div></div><div class="serve-explain-text">Polygon表示地图上的多边形覆盖物,它包含一组点。多边形将这组点按顺序首尾相连,最终围成一个封闭图形。</div><div class="devguide"><div class="leftborderbg" style="height:5000px;"></div><div class="devguideorder"><span>1</span>添加多边形</div><div class="devguidecenter">首先参考展示地图创建基本地图,之后添加多边形覆盖物到地图中。多边形覆盖物可以分别设置描边粗细、颜色、填充颜色等属性。<pre class="prettyprint codestyle">var polygon = new BMapGL.Polygon([ | + | </div></div><div class="bluetitle"><div class="serve-explain-text"><div class="service-page-anchor"><span>添加多边形</span></div></div></div><div class="serve-explain-text">Polygon表示地图上的多边形覆盖物,它包含一组点。多边形将这组点按顺序首尾相连,最终围成一个封闭图形。</div><div class="devguide"><div class="leftborderbg" style="height:5000px;"></div><div class="devguideorder" ><span>1</span>添加多边形</div><div class="devguidecenter" style="width: 680px;">首先参考展示地图创建基本地图,之后添加多边形覆盖物到地图中。多边形覆盖物可以分别设置描边粗细、颜色、填充颜色等属性。<pre class="prettyprint codestyle">var polygon = new BMapGL.Polygon([ |
new BMapGL.Point(116.387112,39.920977), | new BMapGL.Point(116.387112,39.920977), | ||
new BMapGL.Point(116.385243,39.913063), | new BMapGL.Point(116.385243,39.913063), | ||
第83行: | 第83行: | ||
</pre> | </pre> | ||
</div></div> | </div></div> | ||
+ | <div class="bluetitle"><div class="serve-explain-text"><div class="service-page-anchor"><span>覆盖物配置</span></div></div></div><div class="devguide"><div class="devguideorder" style="padding-left: 20px;">1,线、面、圆覆盖物鼠标悬浮图标配置</div><div class="devguidecenter" style="width: 680px;"><pre class="prettyprint codestyle">function overlayCursor(val) { | ||
+ | if (val === 1) { | ||
+ | map.setOverlayMoveCursor('url("https://cdn-app.nio.com/PE/2022/2/23/b7d13dfd-b564-4a94-a3ae-7a1e6741daab.svg") 40 53, crosshair'); | ||
+ | } else { | ||
+ | map.setOverlayMoveCursor('pointer'); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | </div> | ||
+ | </div> | ||
+ | <div class="devguide"><div class="devguideorder" style="padding-left: 20px;">2,点、线、面覆盖物添加geojson转换以及geojson更新数据</div><div class="devguidecenter" style="width: 680px;"><pre class="prettyprint codestyle">var _marker = new BMapGL.Marker(new BMapGL.Point(116.405, 39.920)); | ||
+ | var _mGeojson = _marker.toGeoJSON(); | ||
+ | console.log(_mGeojson); | ||
+ | _mGeojson.geometry.coordinates = [116.486409, 39.921489]; | ||
+ | _marker.updateByGeoJSON(_mGeojson); | ||
+ | // 线、面类似操作 | ||
+ | </pre></div></div><div class="devguide"><div class="devguideorder" style="padding-left: 20px;">3,线、面、圆编辑节点可配置图标</div><div class="devguidecenter" style="width: 680px;"><pre class="prettyprint codestyle" style="overflow-y: auto;">var nodeT = new BMapGL.Icon('http://maponline0.bdimg.com/sty/map_icons2x/MapRes/ditie_beijing_2.png', new BMapGL.Size(11, 11), { | ||
+ | offset: new BMapGL.Size(5, 5) | ||
+ | }); | ||
+ | var node = new BMapGL.Icon('http://maponline0.bdimg.com/sty/map_icons2x/MapRes/zhongheyiyuan.png', new BMapGL.Size(11, 11), { | ||
+ | offset: new BMapGL.Size(5, 5) | ||
+ | }); | ||
+ | var polygon = new BMapGL.Polygon([ | ||
+ | new BMapGL.Point(116.387112, 39.920977), | ||
+ | new BMapGL.Point(116.385243, 39.913063), | ||
+ | new BMapGL.Point(116.394226, 39.917988), | ||
+ | new BMapGL.Point(116.401772, 39.921364), | ||
+ | new BMapGL.Point(116.41248, 39.927893) | ||
+ | ], { | ||
+ | node:node, // 线节点 | ||
+ | nodeT:nodeT, // 线段中间节点 | ||
+ | strokeStyle:'dashed', | ||
+ | strokeColor: 'blue', | ||
+ | strokeWeight: 8, | ||
+ | strokeOpacity: 0.8, | ||
+ | }); | ||
+ | </pre></div></div><div class="devguide"><div class="devguideorder" style="padding-left: 20px;">4,线、面、圆边可设置图片纹理</div><div class="devguidecenter" style="width: 680px;"><pre class="prettyprint codestyle" style="overflow-y: auto;">var polyline = new BMapGL.Polyline([ | ||
+ | new BMapGL.Point(116.399, 39.910), | ||
+ | new BMapGL.Point(116.405, 39.920), | ||
+ | new BMapGL.Point(116.423493, 39.907445) | ||
+ | ], { | ||
+ | strokeTexture: { // width/height 需要是2的n次方 | ||
+ | url:'./images/Icon_road_blue_arrow.png', | ||
+ | width: 16, | ||
+ | height: 64 | ||
+ | }, | ||
+ | strokeWeight: 8, | ||
+ | strokeOpacity: 0.8 | ||
+ | }); | ||
+ | map.addOverlay(polyline); | ||
+ | </pre></div></div> |
2022年11月2日 (三) 14:26的最后版本
所有叠加或覆盖到地图的内容,我们统称为地图覆盖物。覆盖物拥有自己的地理坐标,当您拖动或缩放地图时,它们会相应的移动。
目前JSAPI GL版支持的覆盖物以基本图形为主。本节重点介绍一下如何向地图添加标注,以及与地图相关的一些交互。
可以使用map.addOverlay方法向地图添加覆盖物,使用map.removeOverlay方法移除覆盖物。
覆盖物 | 类名 | 说明 |
抽象基类 | Overlay | 所有的覆盖物均继承此类的方法 |
点 | Marker | 表示地图上的点,可自定义标注的图标 |
折线 | Polyline | 表示地图上的折线 |
多边形 | Polygon | 表示地图上的多边形 |
圆 | Circle | 表示地图上的圆 |
Marker是一个用来往地图上添加点标记的类。使用它将任何你希望用户看到的兴趣点标注在地图上。
API提供了默认图标样式,您也可以通过Icon类来指定自定义图标。Marker的构造函数的参数为Point和MarkerOptions(可选)。
注意:当您使用自定义图标时,标注的地理坐标点将位于标注所用图标的中心位置,您可通过Icon的offset属性修改标定位置。
var point = new BMapGL.Point(116.404, 39.915); var marker = new BMapGL.Marker(point); // 创建标注 map.addOverlay(marker); // 将标注添加到地图中
定义标注图标 通过Icon类可实现自定义标注的图标,下面示例通过参数MarkerOptions的icon属性进行设置,您也可以使用marker.setIcon()方法。
var myIcon = new BMapGL.Icon("markers.png", new BMapGL.Size(23, 25), { // 指定定位位置。 // 当标注显示在地图上时,其所指向的地理位置距离图标左上 // 角各偏移10像素和25像素。您可以看到在本例中该位置即是 // 图标中央下端的尖角位置。 anchor: new BMapGL.Size(10, 25), // 设置图片偏移。 // 当您需要从一幅较大的图片中截取某部分作为标注图标时,您 // 需要指定大图的偏移位置,此做法与css sprites技术类似。 imageOffset: new BMapGL.Size(0, 0 - 25) // 设置图片偏移 }); // 创建标注对象并添加到地图 var marker = new BMapGL.Marker(point, {icon: myIcon}); map.addOverlay(marker);
JSAPI支持监听覆盖物的事件。
marker.addEventListener("click", function(){ alert("您点击了标注"); });
var polyline = new BMapGL.Polyline([ new BMapGL.Point(116.399, 39.910), new BMapGL.Point(116.405, 39.920), new BMapGL.Point(116.425, 39.900) ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); map.addOverlay(polyline);
var polygon = new BMapGL.Polygon([ new BMapGL.Point(116.387112,39.920977), new BMapGL.Point(116.385243,39.913063), new BMapGL.Point(116.394226,39.917988), new BMapGL.Point(116.401772,39.921364), new BMapGL.Point(116.41248,39.927893) ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); map.addOverlay(polygon);
function overlayCursor(val) { if (val === 1) { map.setOverlayMoveCursor('url("https://cdn-app.nio.com/PE/2022/2/23/b7d13dfd-b564-4a94-a3ae-7a1e6741daab.svg") 40 53, crosshair'); } else { map.setOverlayMoveCursor('pointer'); } }
var _marker = new BMapGL.Marker(new BMapGL.Point(116.405, 39.920)); var _mGeojson = _marker.toGeoJSON(); console.log(_mGeojson); _mGeojson.geometry.coordinates = [116.486409, 39.921489]; _marker.updateByGeoJSON(_mGeojson); // 线、面类似操作
var nodeT = new BMapGL.Icon('http://maponline0.bdimg.com/sty/map_icons2x/MapRes/ditie_beijing_2.png', new BMapGL.Size(11, 11), {offset: new BMapGL.Size(5, 5) }); var node = new BMapGL.Icon('http://maponline0.bdimg.com/sty/map_icons2x/MapRes/zhongheyiyuan.png', new BMapGL.Size(11, 11), { offset: new BMapGL.Size(5, 5) }); var polygon = new BMapGL.Polygon([ new BMapGL.Point(116.387112, 39.920977), new BMapGL.Point(116.385243, 39.913063), new BMapGL.Point(116.394226, 39.917988), new BMapGL.Point(116.401772, 39.921364), new BMapGL.Point(116.41248, 39.927893) ], { node:node, // 线节点 nodeT:nodeT, // 线段中间节点 strokeStyle:'dashed', strokeColor: 'blue', strokeWeight: 8, strokeOpacity: 0.8, });
var polyline = new BMapGL.Polyline([new BMapGL.Point(116.399, 39.910), new BMapGL.Point(116.405, 39.920), new BMapGL.Point(116.423493, 39.907445) ], { strokeTexture: { // width/height 需要是2的n次方 url:'./images/Icon_road_blue_arrow.png', width: 16, height: 64 }, strokeWeight: 8, strokeOpacity: 0.8 }); map.addOverlay(polyline);