leaflet入门一

首先为页面做准备

--引入在文档头部leaflet的css文件:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" />

  --紧接着引入leaflet的JS文件:

<!-- Make sure you put this AFTER Leaflet's CSS -->

<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js" ></script>

  --在你希望引入地图的位置处键入一个有id的div:

<div id="mapid"></div>

  --请确定你需要的div有明确的高度,例如将样式写在css里:

#mapid { height: 180px; }

 js

<!--创建地图-->
    var map = L.map('mapid').setView([51.505, -0.09], 13);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    //添加标记
    L.marker([39.9788, 116.30226]).addTo(map)
        .bindPopup("北京大厦<br>").openPopup();

    //圆
    L.circle([39.9908, 116.26625], 500, {
        color: 'red',
        fillColor: '#f03',
        fillOpacity: 0.5
    }).addTo(map).bindPopup("颐和园欢迎你");

    // 多边形很简单:
    L.polygon([
        [39.92096, 116.38591],
        [39.91079, 116.38676],
        [39.91118, 116.3962],
        [39.92014, 116.39482]
    ]).addTo(map).bindPopup("故宫");

    //openPopup是表示默认是否打开)
    /*marker.bindPopup("北京大厦").openPopup();
    circle.bindPopup("颐和园");
    polygon.bindPopup("故宫");*/

    var popup = L.popup()
        .setLatLng([39.9908, 116.26625])
        .setContent("I am a standalone popup.")
        .openOn(map);

    var popup = L.popup();

    //点击map弹出提示框
    function onMapClick(e) {
        popup
            .setLatLng(e.latlng)
            .setContent("You clicked the map at " + e.latlng.toString())
            .openOn(map);
    }

    map.on('click', onMapClick);

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值