欢迎光临
我们一直在努力

九八云百度小程序教程:map 地图

  • map 地图
    • 属性说明
    • 示例
      • 代码示例 1
      • 代码示例 2:原生组件设置 border 无效,也不可用 cover-view 覆盖为圆角
    • marker
      • 属性说明
      • 示例
    • polyline
      • 属性说明
      • 示例
    • polygon
      • 属性说明
      • 示例
    • circle
      • 属性说明
      • 示例
    • control
      • 属性说明
      • 示例
    • Bug & Tip

    map 地图

    解释:地图。客户端创建的原生组件,使用时请注意相关限制。

    属性说明

    属性名 类型 默认值 必填 说明 最低版本 Web 态说明
    longitude Number 北京经度 gcj02 坐标系中心经度
    latitude Number 北京纬度 gcj02 坐标系中心纬度
    scale Number 16 缩放级别,取值范围为 4-21 。动态设置 scale 值用法:scale=”{= scale =}”
    markers Array.<marker> 标记点。详情请参考 marker
    polyline Array.<polyline> 路线。详情请参考 polyline
    polygons Array.<polygon> 多边形(工具暂不支持)。详情请参考 polygon 2.0.13
    低版本请做兼容性处理
    circles Array.<circle> 圆。详情请参考 circle
    controls Array.<control> 控件。详情请参考 control
    include-points Array.<point> 缩放视野以包含所有给定的坐标点
    show-location Boolean false 显示带有方向的当前定位点 暂时不能显示方向
    enable-3D Boolean false 显示 3D 楼块(工具暂不支持) 2.0.13
    低版本请做兼容性处理
    暂不支持
    show-compass Boolean false 显示指南针(工具暂不支持) 2.0.13
    低版本请做兼容性处理
    暂不支持
    enable-overlooking Boolean false 开启俯视(工具暂不支持) 2.0.13
    低版本请做兼容性处理
    暂不支持
    enable-zoom Boolean true 是否支持缩放(工具暂不支持) 2.0.13
    低版本请做兼容性处理
    enable-scroll Boolean true 是否支持拖动(工具暂不支持) 2.0.13
    低版本请做兼容性处理
    enable-rotate Boolean false 是否支持旋转(工具暂不支持) 2.0.13
    低版本请做兼容性处理
    暂不支持
    bindmarkertap EventHandle 点击标记点时触发
    bindcallouttap EventHandle 点击标记点对应的气泡时触发
    bindcontroltap EventHandle 点击控件时触发
    bindregionchange EventHandle 视野发生变化时触发
    bindtap EventHandle 点击地图时触发
    bindupdated EventHandle 在地图渲染更新完成时触发
    bindpoitap EventHandle 点击地图 poi 点时触发 暂不支持

    示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    扫码体验

    代码示例

    请使用百度APP扫码

    代码示例 1

    • SWAN
    • JS
    
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <map class="map"
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}"
    6. scale="{{scale}}"
    7. markers="{{markers}}"
    8. polyline="{{polyline}}"
    9. polygons="{{drawPolygon ? polygons : []}}"
    10. circles="{{circles}}"
    11. controls="{{controls}}"
    12. show-location="{{showLocation}}"
    13. include-points="{{includePoints}}"
    14. enable-3D="{{enable3d}}"
    15. show-compass="{{showCompass}}"
    16. enable-overlooking="{{enableOverlooking}}"
    17. enable-zoom="{{enableZoom}}"
    18. enable-scroll="{{enableScroll}}"
    19. enable-rotate="{{enableRotate}}"
    20. bindmarkertap="onMarkertap"
    21. bindcallouttap="onCallouttap"
    22. bindcontroltap="onControltap"
    23. bindregionchange="onRegionchange"
    24. bindtap="onTap"
    25. bindupdated="onUpdated"
    26. bindpoitap="onPoitap">
    27. </map>
    28. <view class="item-scroll block border-bottom">
    29. <text class="switch-text">支持缩放</text>
    30. <switch bindchange="toggleZoom" checked></switch>
    31. </view>
    32. <view class="item-scroll block border-bottom">
    33. <text class="switch-text">支持拖动</text>
    34. <switch bindchange="toggleScroll" checked></switch>
    35. </view>
    36. <view class="item-scroll block border-bottom" s-if="{{!isWeb}}">
    37. <text class="switch-text">支持旋转</text>
    38. <switch bindchange="toggleRotate" checked></switch>
    39. </view>
    40. </view>
    41. <view class="tip-week">工具暂不支持手势控制</view>
    42. </view>
    
     
    1. Page({
    2. data: {
    3. latitude: '40.048828',
    4. longitude: '116.280412',
    5. scale: 16,
    6. isWeb: false,
    7. polygons: [],
    8. drawPolygon: false,
    9. includePoints: [],
    10. showLocation: false,
    11. enable3d: false,
    12. showCompass: false,
    13. enableOverlooking: false,
    14. enableZoom: true,
    15. enableScroll: true,
    16. enableRotate: true,
    17. markers: [{
    18. markerId: '1',
    19. latitude: '40.052751',
    20. longitude: '116.278796'
    21. }, {
    22. markerId: '2',
    23. latitude: '40.048828',
    24. longitude: '116.280412',
    25. title: 'markerId: 2',
    26. zIndex: 100,
    27. iconPath: 'https://b.bdstatic.com/searchbox/icms/searchbox/img/demo_location.png',
    28. rotate: 0,
    29. width: 30,
    30. height: 50,
    31. callout: {
    32. display: 'ALWAYS',
    33. content: '百度科技园',
    34. color: '#000',
    35. fontSize: '16',
    36. borderRadius: 50,
    37. bgColor: '#5B9FFF',
    38. padding: 2,
    39. textAlign: 'center'
    40. }
    41. }, {
    42. markerId: '3',
    43. latitude: '40.049655',
    44. longitude: '116.27505',
    45. callout: {
    46. display: 'ALWAYS',
    47. content: '西山壹号院'
    48. }
    49. }],
    50. polyline: [{
    51. points: [{
    52. longitude: 116.278796,
    53. latitude: 40.048828
    54. }, {
    55. longitude: 116.27505,
    56. latitude: 40.049655
    57. }],
    58. color: '#FF5F41FF',
    59. width: 2,
    60. dottedLine: true
    61. }],
    62. controls: [{
    63. controlId: 1,
    64. iconPath: 'https://b.bdstatic.com/searchbox/icms/searchbox/img/api_logo.png',
    65. position: {
    66. left: 0,
    67. top: 100,
    68. width: 50,
    69. height: 50
    70. },
    71. clickable: true
    72. }],
    73. circles: [{
    74. latitude: '40.052751',
    75. longitude: '116.278796',
    76. color: '#FF5F41FF',
    77. fillColor: '#21FFFFFF',
    78. radius: '200',
    79. strokeWidth: '2'
    80. }]
    81. },
    82. showLocation() {
    83. this.setData({
    84. showLocation: !this.data.showLocation
    85. });
    86. },
    87. toggle3d() {
    88. this.setData({
    89. enable3d: !this.data.enable3d
    90. });
    91. },
    92. toggleShowCompass() {
    93. this.setData({
    94. showCompass: !this.data.showCompass
    95. });
    96. },
    97. toggleOverlooking() {
    98. this.setData({
    99. enableOverlooking: !this.data.enableOverlooking
    100. });
    101. },
    102. toggleZoom() {
    103. this.setData({
    104. enableZoom: !this.data.enableZoom
    105. });
    106. },
    107. toggleScroll() {
    108. this.setData({
    109. enableScroll: !this.data.enableScroll
    110. });
    111. },
    112. toggleRotate() {
    113. this.setData({
    114. enableRotate: !this.data.enableRotate
    115. });
    116. },
    117. togglePolygon() {
    118. this.setData({
    119. drawPolygon: !this.data.drawPolygon
    120. });
    121. },
    122. onMarkertap(e) {
    123. console.log('onMarkertap callback:', e);
    124. },
    125. onCallouttap(e) {
    126. console.log('onCallouttap callback:', e);
    127. },
    128. onControltap(e) {
    129. console.log('onControltap callback:', e);
    130. },
    131. onRegionchange(e) {
    132. console.log('onRegionchange callback:', e);
    133. },
    134. onTap(e) {
    135. console.log('onTap callback:', e);
    136. },
    137. onUpdated(e) {
    138. console.log('onUpdated callback:', e);
    139. },
    140. onPoitap(e) {
    141. console.log('onPoitap callback:', e);
    142. },
    143. /**
    144. * 点击标记点时触发的事件
    145. */
    146. bindmarkertap() {
    147. swan.showToast({
    148. title: '点击标记啦',
    149. icon: 'none'
    150. });
    151. },
    152. });

    代码示例 2:原生组件设置 border 无效,也不可用 cover-view 覆盖为圆角

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • CSS
    
     
    1. <view class="wrap">
    2. <cover-view class="card-area">
    3. <map
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}">
    6. </map>
    7. </cover-view>
    8. </view>
    
     
    1. .card-area {
    2. width: 3.88rem;
    3. height: 2.18rem;
    4. }

    marker

    解释:标记点,用于在地图上显示标记的位置。

    属性说明

    属性名 说明 类型 必填 备注
    markerId 标记点 id Number marker 点击事件回调会返回此 id 。建议为每个 marker 设置 Number 类型的 id ,保证更新 marker 时有更好的性能
    latitude 纬度 Number 浮点数,范围 -90~90
    longitude 经度 Number 浮点数,范围 -180~180
    zIndex 显示层级 Number iOS 地图不支持设置此属性
    iconPath 显示的图标 String 项目目录下的图片路径,支持网络路径、本地路径(相对和绝对路径写法),工具暂不支持绝对路径写法
    rotate 旋转角度 Number 默认为 0 ,顺时针旋转的角度,范围 0~360
    alpha 标注的透明度 Number 默认为 1 ,无透明,范围 0~1
    width 标注图标宽度 Number 默认为图片实际宽度
    height 标注图标高度 Number 默认为图片实际高度
    callout 自定义标记点上方的气泡窗口 Object 支持的属性见下表,可识别换行符
    label 为标记点旁边增加标签。暂不支持换行 Object 支持的属性见下表,可识别换行符
    anchor 经纬度在标注图标的锚点 Object 默认底边中点,{x, y}:x 表示横向(0-1),y 表示竖向(0-1)。{x: .5, y: 1}表示底边中点

    callout 属性说明

    属性名 说明 类型 备注
    content 文本 String
    color 文本颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    fontSize 文字大小 Number
    borderWidth 边框宽度 Number
    borderColor 边框颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    borderRadius callout 边框圆角 Number
    bgColor 背景色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    padding 文本边缘留白 Number
    display ‘BYCLICK’:点击显示;’ALWAYS’:常显 String 默认为常显
    textAlign 文本对齐方式。有效值:left、right、center String 默认为居中对齐

    label 属性说明

    属性名 说明 类型 备注
    content 文本 String
    color 文本颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    fontSize 文字大小 Number
    x label 的坐标,原点是 marker 对应的经纬度 Number
    y label 的坐标,原点是 marker 对应的经纬度 Number
    borderWidth 边框宽度 Number
    borderColor 边框颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    borderRadius 边框圆角 Number
    bgColor 背景色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    padding 文本边缘留白 Number
    textAlign 文本对齐方式。有效值:left、right、center String 默认为居中对齐

    示例

    图片示例

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
    
     
    1. <view class="wrap">
    2. <map
    3. style="width: 100%; height: 300px;"
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}"
    6. markers="{{markers}}"
    7. bindmarkertap="bindmarkertap"
    8. bindcallouttap="bindcallouttap">
    9. </map>
    10. </view>
    
     
    1. Page({
    2. data: {
    3. latitude: 40.048828,
    4. longitude: 116.280412,
    5. markers: [{
    6. markerId: 1,
    7. latitude: 40.048828,
    8. longitude: 116.280412,
    9. title: 'markerId: 1',
    10. zIndex: 100,
    11. iconPath: '../images/location.png',
    12. rotate: 90,
    13. callout: {
    14. display: 'ALWAYS',
    15. content: '百度科技园',
    16. color: '#000',
    17. fontSize: '16',
    18. borderRadius: 50,
    19. bgColor: '#5B9FFF',
    20. padding: 2,
    21. textAlign: 'center'
    22. }
    23. }, {
    24. markerId: 2,
    25. latitude: 40.049655,
    26. longitude: 116.27505,
    27. title: 'markerId: 2',
    28. zIndex: 100,
    29. rotate: 90,
    30. alpha: 0.5,
    31. width: 30,
    32. height: 50,
    33. label: {
    34. 'content': 'label',
    35. 'color': '#7B68EE',
    36. 'fontSize': 16,
    37. 'borderWidth': 1,
    38. 'borderColor': '#5B9FFF',
    39. 'borderRadius': 50,
    40. 'bgColor': '#ADCFFF',
    41. 'padding': 5,
    42. 'textAlign': 'center'
    43. },
    44. anchor: {x: .5, y: 1},
    45. callout: {
    46. display: 'BYCLICK',
    47. content: '西山壹号院',
    48. color: '#FFF',
    49. fontSize: '16',
    50. borderRadius: 50,
    51. bgColor: '#5B9FFF',
    52. padding: 2,
    53. textAlign: 'center'
    54. }
    55. }]
    56. },
    57. bindmarkertap() {
    58. swan.showToast({
    59. title: '点击标记啦',
    60. icon: 'none'
    61. });
    62. },
    63. bindcallouttap() {
    64. swan.showToast({
    65. title: '点击标记上方气泡啦',
    66. icon: 'none'
    67. });
    68. }
    69. });

    polyline

    解释:指定一系列坐标点,从数组第一项连线至最后一项。

    属性说明

    属性名 说明 类型 必填 备注
    points 经纬度数组 Array [{latitude: 0, longitude: 0}]
    color 线的颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    width 线的宽度 Number
    dottedLine 是否虚线 Boolean 默认 false
    arrowLine 带箭头的线 Boolean 默认 false
    arrowIconPath 更换箭头图标 String 在 arrowLine 为 true 时生效
    borderColor 线的边框颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    borderWidth 线的厚度 Number

    示例

    图片示例

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
    
     
    1. <view class="wrap">
    2. <map
    3. style="width: 100%; height: 300px;"
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}"
    6. polyline="{{polyline}}">
    7. </map>
    8. </view>
    
     
    1. Page({
    2. data: {
    3. latitude: 40.048828,
    4. longitude: 116.280412,
    5. polyline: [{
    6. points: [{
    7. longitude: 116.278796,
    8. latitude: 40.048828
    9. }, {
    10. longitude: 116.27505,
    11. latitude: 40.049655
    12. }],
    13. color: '#000000AA',
    14. width: 2,
    15. dottedLine: true,
    16. arrowLine: true,
    17. // arrowIconPath: '开发者服务器图片路径',
    18. borderColor: '#FFB6C1',
    19. borderWidth: 5
    20. }]
    21. }
    22. });

    polygon

    解释:指定一系列坐标点,根据 points 坐标数据生成闭合多边形。

    属性说明

    属性名 说明 类型 必填 备注
    points 经纬度数组 Array [{latitude: 0, longitude: 0}]
    strokeWidth 描边的宽度 Number
    strokeColor 描边的颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    fillColor 填充颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    zIndex 设置多边形 Z 轴数值 Number

    示例

    图片示例

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
    
     
    1. <view class="wrap">
    2. <map
    3. style="width: 100%; height: 300px;"
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}"
    6. polygons="{{polygons}}">
    7. </map>
    8. </view>
    
     
    1. Page({
    2. data: {
    3. latitude: 40.048828,
    4. longitude: 116.280412,
    5. polygons: [{
    6. points: [{
    7. longitude: 116.278796,
    8. latitude: 40.048828
    9. }, {
    10. longitude: 116.27505,
    11. latitude: 40.049655
    12. },{
    13. longitude: 116.27305,
    14. latitude: 40.050655
    15. },{
    16. longitude: 116.27105,
    17. latitude: 40.051655
    18. }],
    19. strokeWidth: 1,
    20. strokeColor: '#000000AA',
    21. fillColor: '#000000AA',
    22. zIndex: 20
    23. }]
    24. }
    25. });

    circle

    解释:在地图上显示圆

    属性说明

    属性名 说明 类型 必填 备注
    latitude 纬度 Number 浮点数,范围 -90~90
    longitude 经度 Number 浮点数,范围 -180~180
    color 描边的颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    fillColor 填充颜色 String 8 位十六进制表示,最后 2 位表示 alpha 值(可省略)
    radius 半径 Number
    strokeWidth 描边的宽度 Number

    示例

    图片示例

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
    
     
    1. <view class="wrap">
    2. <map
    3. style="width: 100%; height: 300px;"
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}"
    6. circles="{{circles}}">
    7. </map>
    8. </view>
    
     
    1. Page({
    2. data: {
    3. scale: 16,
    4. latitude: 40.048828,
    5. longitude: 116.280412,
    6. circles: [{
    7. latitude: 40.052751,
    8. longitude: 116.278796,
    9. color: '#FF5F41FF',
    10. fillColor: '#FF5F41FF',
    11. radius: 200,
    12. strokeWidth: 2
    13. }]
    14. }
    15. });

    control

    解释:在地图上显示控件,控件不随着地图移动。

    属性说明

    属性名 说明 类型 必填 备注
    controlId 控件 id Number 在控件点击事件回调会返回此 id
    position 控件在地图的位置 Object 控件相对地图位置
    iconPath 显示的图标 String 项目目录下的图片路径,支持网络路径、本地路径(相对和绝对路径写法),工具暂不支持绝对路径写法
    clickable 是否可点击 Boolean 默认不可点击

    position 属性说明

    属性名 说明 类型 必填 备注
    left 距离地图的左边界多远 Number 默认为 0
    top 距离地图的上边界多远 Number 默认为 0
    width 控件宽度 Number 默认为图片宽度
    height 控件高度 Number 默认为图片高度

    示例

    图片示例

    代码示例

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
    
     
    1. <view class="wrap">
    2. <map
    3. style="width: 100%; height: 300px;"
    4. longitude="{{longitude}}"
    5. latitude="{{latitude}}"
    6. controls="{{controls}}"
    7. bindcontroltap="bindcontroltap"
    8. >
    9. </map>
    10. </view>
    
     
    1. Page({
    2. data: {
    3. latitude: 40.048828,
    4. longitude: 116.280412,
    5. controls: [{
    6. controlId: 1,
    7. iconPath: '/images/navigate.png',
    8. position: {
    9. left: 0,
    10. top: 100,
    11. width: 50,
    12. height: 50
    13. },
    14. clickable: true
    15. }]
    16. },
    17. bindcontroltap() {
    18. swan.showToast({
    19. title: '点击控件',
    20. icon: 'none'
    21. })
    22. }
    23. });

    Bug & Tip

    • Tip:map 组件是由客户端创建的原生组件,它的层级是最高的,不能通过 z-index 控制层级。
    • Tip:请勿在 scroll-view、swiper、picker-view、movable-view 中使用 map 组件。
    • Tip:CSS 动画对 map 组件无效。
    • Tip:cover-view、cover-image 组件可覆盖在 map 组件之上。
    • Tip:Android 与 iOS 定位精度不同,双端定位存在差异。
    • Tip:map 组件使用的经纬度是火星坐标系,调用 swan.getLocation 接口需要指定 type 为 gcj02 。
    • Tip:开发者工具由于坐标系不同,定位与客户端存在差异。开发时请以客户端为准。
    • Bug:Web 态由于坐标系不同,定位与客户端存在差异,坐标与客户端相比有一定偏移。待后续版本修复。

    赞(0) 打赏
    未经允许不得转载:九八云安全 » 九八云百度小程序教程:map 地图

    评论 抢沙发