当前位置:   article > 正文

uniapp 3D地图 效果模拟 mapbox 或者 其他设备_uniapp小程序使用mapbox

uniapp小程序使用mapbox

一  ,   mapbox 地图  的引入和安装

  

(1)先进行mapbox 的引入安装      npm install --save mapbox-gl

(2)mapbox  gl  js  官方文档   

Markers and controls | Mapbox GL JS | Mapboxicon-default.png?t=N7T8https://docs.mapbox.com/mapbox-gl-js/api/markers/

(3)中文翻译  使用方式 下边是百度翻译的连接:

百度翻译-200种语言互译、沟通全世界! (baidu.com)icon-default.png?t=N7T8http://fanyi.baidu.com/transpage?query=https://www.mapbox.com/&source=url&ie=utf8&from=auto&to=zh&render=1&origin=ps

 

二  , 3d 地图的 script 的 定义方式

  module="mapContainer" lang="renderjs" 

代码展示

  1. <template>
  2. <view>
  3. <div class="mapDiv" ref="mapContainer" id="mapContainer"> </div>
  4. </view>
  5. </template>
  6. mapContainer 这个也可以定义成任何名字 但是要统一
  7. <script setup module="mapContainer" lang="renderjs">
  8. import mapboxgl from 'mapbox-gl';
  9. export default {
  10. mounted(){
  11. const map = new mapboxgl.Map({
  12. accessToken: 'pk.eyJ1IAQ', //换成自己数据
  13. container: 'mapContainer', //换成自己数据
  14. style: 'mapbox://styl1', //换成自己数据
  15. //style: strStyle, //换成自己数据
  16. center: [125.05, 44.0], //换成自己数据
  17. maxBounds: [
  18. [121.6383441, 40.864816],
  19. [131.34852, 46.186],
  20. ],
  21. maxZoom: 187,
  22. minZoom: 97,
  23. zoom: 172,
  24. pitch: 475, //俯视角度
  25. //bearing: -670,
  26. antialias: false,
  27. attributionControl: false,
  28. });
  29. map.on('load',()=>{
  30. console.log("load",3333333)
  31. })
  32. },
  33. }
  34. </script>
  35. <style>
  36. ._abbr{
  37. font-size: 24px;
  38. margin-top: 200px;
  39. }
  40. .mapDiv {
  41. height: 1280px;
  42. width: 100%;
  43. }
  44. .login-btn {
  45. margin: 0 43%;
  46. width: 150px;
  47. height: 150px;
  48. background: #004d00;
  49. border-radius: 100px;
  50. color: #fff;
  51. font-size: 25px;
  52. text-align: center;
  53. line-height: 150px;
  54. position: fixed;
  55. bottom: 50px;
  56. }
  57. .abb{
  58. display: flex;
  59. padding:5px 47%;
  60. }
  61. </style>

三  , 获取定位置   添加标记点

代码展示

  1. <template>
  2. <view>
  3. <div class="mapDiv" ref="mapContainer" id="mapContainer"> </div>
  4. </view>
  5. </template>
  6. <script setup module="mapContainer" lang="renderjs">
  7. import mapboxgl from 'mapbox-gl';
  8. export default {
  9. mounted(){
  10. const map = new mapboxgl.Map({
  11. accessToken: 'pk.eyJ1IAQ', //换成自己数据
  12. container: 'mapContainer', //换成自己数据
  13. style: 'mapbox://styl1', //换成自己数据
  14. //style: strStyle, //换成自己数据
  15. center: [125.05, 44.0], //换成自己数据
  16. maxBounds: [
  17. [121.6383441, 40.864816],
  18. [131.34852, 46.186],
  19. ],
  20. maxZoom: 187,
  21. minZoom: 97,
  22. zoom: 172,
  23. pitch: 475, //俯视角度
  24. //bearing: -670,
  25. antialias: false,
  26. attributionControl: false,
  27. });
  28. const geolocate = new mapboxgl.GeolocateControl({ //获取当前位置信息
  29. positionOptions: {
  30. enableHighAccuracy: true
  31. },
  32. trackUserLocation: true
  33. });
  34. // Add the control to the map.
  35. map.addControl(geolocate);
  36. map.on('load', () => {
  37. geolocate.trigger();
  38. });
  39. //========================================
  40. 获取点击位置的信息
  41. map.on('click', function(e) { //获取位置信息
  42. var coordinates = e.lngLat;
  43. const longitude = coordinates.lat
  44. console.log(coordinates.lat,'111') //经纬度
  45. console.log(coordinates.lng,'111')
  46. // Set marker options.
  47. const marker = new mapboxgl.Marker({ //标记点
  48. draggable: true //是否移动
  49. })
  50. .setLngLat( e.lngLat) //定位信息
  51. .setPopup(new mapboxgl.Popup().setHTML("<h1>我是小周啊</h1>")) // 基本信息
  52. .addTo(map); //运行在地图
  53. },
  54. }
  55. </script>
  56. <style>
  57. ._abbr{
  58. font-size: 24px;
  59. margin-top: 200px;
  60. }
  61. .mapDiv {
  62. height: 1280px;
  63. width: 100%;
  64. }
  65. .login-btn {
  66. margin: 0 43%;
  67. width: 150px;
  68. height: 150px;
  69. background: #004d00;
  70. border-radius: 100px;
  71. color: #fff;
  72. font-size: 25px;
  73. text-align: center;
  74. line-height: 150px;
  75. position: fixed;
  76. bottom: 50px;
  77. }
  78. .abb{
  79. display: flex;
  80. padding:5px 47%;
  81. }
  82. </style>

 四  , 要是想要uniapp 自带的获取位置  获取设备地址 不需要配置key信息,要是后续有别的 工作 参考下边配置一下key

代码展示

  1. function openUserPopup() { 获取设备当前位置
  2. console.log('ada')
  3. uni.getLocation ({
  4. type: 'wgs84',
  5. altitude:true ,
  6. geocode:true,
  7. success: function (res) {
  8. kkk.value = res.longitude
  9. lll.value = res.latitude
  10. console.log('当前位置的经度:' + res.longitude);
  11. console.log('当前位置的纬度:' + res.latitude);
  12. },
  13. });
  14. }
  15. -----------------------------------------------------------------------------
  16. uni.chooseLocation({ //获取当前位置 打开地图选择位置。
  17. success: function (res) {
  18. console.log('位置名称:' + res.name);
  19. console.log('详细地址:' + res.address);
  20. console.log('纬度:' + res.latitude);
  21. console.log('经度:' + res.longitude);
  22. }
  23. });
  24. -----------------------------------------------------------------------------------
  25. uni.getLocation({ //打开设备默认地图 获取当前位置
  26. type: 'gcj02', //返回可以用于uni.openLocation的经纬度
  27. success: function (res) {
  28. const latitude = res.latitude;
  29. const longitude = res.longitude;
  30. uni.openLocation({
  31. latitude: latitude,
  32. longitude: longitude,
  33. success: function () {
  34. console.log('success');
  35. }
  36. });
  37. }
  38. });
  39. ----------------------------------------------------------------------------------

--------------------------------------------------------------------------------------

一 , uniapp  腾讯地图的 使用方式  首先打开uniapp  找到  地图选项

配置过程

腾讯位置获取链接

腾讯位置服务 - 立足生态,连接未来 (qq.com)icon-default.png?t=N7T8https://lbs.qq.com/

代码展示

  1. <template>
  2. <view>
  3. <button type="default" @click="getCurrentLocation()">获取当前位置</button>
  4. <button type="primary" @click="getaddress()">获取选择的位置</button>
  5. <view style="color: red;">
  6. {{positionInfo.address}}
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. positionInfo: {
  15. address: '',
  16. longitude: '', //经度
  17. latitude: '', //纬度
  18. },
  19. }
  20. },
  21. methods: {
  22. // 通过自带的方法获取到当前的经纬度,调用方法获取到地址获取到地址的中文信息
  23. getCurrentLocation() {
  24. let that = this //在uniapp中药定义一下this才能使用
  25. uni.getLocation({
  26. type: 'wgs84',
  27. success: function(res) {
  28. console.log(res)
  29. that.positionInfo.longitude = res.longitude;
  30. that.positionInfo.latitude = res.latitude;
  31. that.loAcquire(that.positionInfo.longitude, that.positionInfo.latitude)
  32. }
  33. });
  34. },
  35. // 获取当前地址
  36. loAcquire(longitude, latitude) {
  37. let that = this;
  38. uni.showLoading({
  39. title: '加载中',
  40. mask: true
  41. });
  42. let str = `output=jsonp&key='此处输入你申请的密钥'=${latitude},${longitude}` //记得在这里要输入密钥哦!
  43. this.$jsonp('https://apis.map.qq.com/ws/geocoder/v1/?' + str, {}).then(res => {
  44. console.log(res);
  45. uni.hideLoading();
  46. if (res.status == 0) {
  47. that.positionInfo.address = '当前位置是:' + res.result.address_component.street_number; //当前定位
  48. }
  49. })
  50. },
  51. // 获取选择地址
  52. getaddress() {
  53. let that = this
  54. uni.chooseLocation({
  55. success: function(res) {
  56. that.positionInfo.address = '选择的位置是:' + res.name
  57. }
  58. });
  59. },
  60. }
  61. }
  62. </script>
  63. <style scoped>
  64. button,
  65. view {
  66. margin: 20px;
  67. }
  68. </style>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号