关键词搜索

源码搜索 ×
×

Leaflet文档阅读笔记-Leaflet on Mobile笔记

发布2019-09-07浏览4689次

详情内容

目录

 

CSS和HTML改如何设置

定位功能


 

CSS和HTML改如何设置

如下的设置:

  1. body {
  2. padding: 0;
  3. margin: 0;
  4. }
  5. html, body, #map {
  6. height: 100%;
  7. width: 100vw;
  8. }

这里还要设置禁止浏览器进行不必要的放缩

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

上面是官方的给出的,下面是本人个人的看法,css应该这么写!

  1. <style>
  2. html, body {
  3. height: 100%;
  4. margin: 0;
  5. }
  6. #map {
  7. width: 100%;
  8. height: 100%;
  9. }
  10. </style>

这样,整个屏幕都是地图了

 

定位功能

我先把官方代码贴上了吧

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Mobile tutorial - Leaflet</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
  8. <link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
  9. <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>
  10. <style>
  11. html, body {
  12. height: 100%;
  13. margin: 0;
  14. }
  15. #map {
  16. width: 600px;
  17. height: 400px;
  18. }
  19. </style>
  20. <style>body { padding: 0; margin: 0; } #map { height: 100%; width: 100vw; }</style>
  21. </head>
  22. <body>
  23. <div id='map'></div>
  24. <script>
  25. var map = L.map('map').fitWorld();
  26. L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
  27. maxZoom: 18,
  28. attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
  29. '<a href="https://creativecommons.org/licenses/by-sahttps://cdn.jxasp.com:9143/image/2.0/">CC-BY-SA</a>, ' +
  30. 'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  31. id: 'mapbox.streets'
  32. }).addTo(map);
  33. function onLocationFound(e) {
  34. var radius = e.accuracy / 2;
  35. L.marker(e.latlng).addTo(map)
  36. .bindPopup("You are within " + radius + " meters from this point").openPopup();
  37. L.circle(e.latlng, radius).addTo(map);
  38. }
  39. function onLocationError(e) {
  40. alert(e.message);
  41. }
  42. map.on('locationfound', onLocationFound);
  43. map.on('locationerror', onLocationError);
  44. map.locate({setView: true, maxZoom: 16});
  45. </script>
  46. </body>
  47. </html>

从中可以知道,当定位成功后,会触发locationFound这个信号,当有问题时会触发locationerror信号!

通过onLocationFound的参数,就能获得位置坐标!

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载