mapbox js创建一个简单的demo

注意定位坐标的图片marker是自己下载的

geojson.features..foreach()是操作数据的方法,

popup点击显示弹窗的事件

<!DOCTYPE html>

<html>
<head>
    <meta charset='utf-8' />
    <title></title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.0/mapbox-gl.css' rel='stylesheet' />
    <style>
      body {
        margin: 0;
        padding: 0;
      }


      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
      }
.marker {
 background-image: url('mapbox-icon.png');
 background-size: cover;
 width: 50px;
 height: 50px;
 border-radius: 50%;
 cursor: pointer;
}
.mapboxgl-popup {
 max-width: 200px;
}


.mapboxgl-popup-content {
 text-align: center;
 font-family: 'Open Sans', sans-serif;
}
    </style>
</head>
<body>


<div id='map'></div>


<script>
var geojson = {
  type: 'FeatureCollection',
  features: [{
    type: 'Feature',
    geometry: {
      type: 'Point',
      coordinates: [-77.032, 38.913]
    },
    properties: {
      title: 'Mapbox',
      description: 'Washington, D.C.'
    }
  },
  {
    type: 'Feature',
    geometry: {
      type: 'Point',
      coordinates: [-122.414, 37.776]
    },
    properties: {
      title: 'Mapbox',
      description: 'San Francisco, California'
    }
  }]
};
mapboxgl.accessToken = 'pk.eyJ1IjoiaG91YmlhbyIsImEiOiJjajdnNjVicDYxNHAzMndvMmI4NGgzaTZrIn0.ZvpHO1bVunIhgsBq_nVYFQ';


var map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/mapbox/light-v9',
  center: [-96, 37.8],
  zoom: 3
});
// add markers to map
geojson.features.forEach(function(marker) {


  // create a HTML element for each feature
  var el = document.createElement('div');
  el.className = 'marker';


  // make a marker for each feature and add to the map
  new mapboxgl.Marker(el, { offset: [-50 / 2, -50 / 2] })
  .setLngLat(marker.geometry.coordinates)
  .addTo(map);
 new mapboxgl.Marker(el, { offset: [-50 / 2, -50 / 2] })
  .setLngLat(marker.geometry.coordinates)
  .setPopup(new mapboxgl.Popup({ offset: 25 }) // add popups
  .setHTML('<h3>' + marker.properties.title + '</h3><p>' + marker.properties.description + '</p>'))
  .addTo(map);
});


// code from the next step will go here!
</script>
</body>

</html>

样式规范中的Layers。
发布会文章介绍mapbox的强大
http://www.pingwest.com/demo/mapbox/


官方文档的----中文版本(来自微信公众号)
https://www.mapbox.cn/mapbox-gl-js/api/
基本api的使用情况



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值