個人申請高德地圖key時無法使用AMap.DistrictSearch,可以通過poi數據繪制省市縣區塊
1.進入POI數據網站找到需要的省市縣,下載對應的GeoJson文件?,此處為poi數據網站鏈接
2.?
處理geoJson數據,可以直接新建json文件,將下載的geojson內容復制進入也可以使用。也可以在項目內webpack配置文件中修改,同時確保安裝了json-loader。我一般都用前者就足夠了
npm install json-loader --save-dev
module: {rules: [{test: /\.geojson$/,loader: 'json-loader'}]
}
.3.將文件放入項目內,從組件中引入json文件
import areaList from "../../map/beijing.json";
4.獲取點位坐標
//繪制地圖區域mapArea(){let _this = thislet polygon;var points=[];areaList.features.forEach(i=>{points=[[]];i.geometry.coordinates[0][0].forEach(item=>{points[0].push(new AMap.LngLat(item[0],item[1]));// points.push([item[0],item[1]]);})})return points},
5.生成地圖
initMap(){let _this = this;let mask = this.mapArea()_this.map = new AMap.Map('mapBox', {mask: mask,center: [110.051784, 34.74073],expandZoomRange: true,disableSocket: true,viewMode: '3D',showLabel: true,labelzIndex: 130,zoom: 10.2,layers: [new AMap.TileLayer.RoadNet({}),new AMap.TileLayer.Satellite()]});var maskerIn = new AMap.Marker({position: [116.501415, 39.926055],map: _this.map})var maskerOut = new AMap.Marker({//區域外的不會顯示position: [117.001415, 39.926055],map: _this.map})var height = -5000;var color = '#0088ffcc';//rgba// 添加描邊new AMap.Polyline({path: mask[0],strokeColor: '#99ffff',strokeWeight: 6,map: _this.map})},