vue中调用地图

一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 

这个就不细说了,按照其文档,就能够安装下来。

二. 按照官方提供的方法引入

1.修改webpac.base.conf.js文件

 

  externals: {
    'AMap': 'AMap'
  }

 

2.引入sdk
引入有两种方式,一种是页面直接引入

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=您申请的key值"></script> 

还有一种是异步加载

复制代码
<script src="http://webapi.amap.com/maps?v=1.3&amp;key=您申请的key值&callback=init"></script>
<script>
    function init(){
        var map = new AMap.Map('container', {
            center: [117.000923, 36.675807],
            zoom: 6
        });
        map.plugin(["AMap.ToolBar"], function() {
            map.addControl(new AMap.ToolBar());
        });
    }
</script>
复制代码

需要注意的是:

你也可以去动态去创造,例如这样

var script = document.createElement('script')
script.type = 'text/javascript'
script.src = 'https://webapi.amap.com/maps?v=1.3&key=yourKey'   // 高德地图
document.body.appendChild(script)

 不管是采用哪种方式,都要保证你想要加载地图的js文件,在引入的sdk之后

这样,在第三步的时候,才不会报错

 三. 在当前需要加载vue页面引入

  import AMap from 'AMap'

四. 页面实例

这是初始化地图,并且调用插件的代码(map.vue)如:

复制代码
<template>
  <div>
    <div id="container" style="width:500px; height:300px"></div>
  </div>
</template>
<script>
  import AMap from 'AMap'
  var map
  export default {
    mounted: function () {
      this.init()
    },
    methods: {
      init: function () {
        map = new AMap.Map('container', {
          center: [116.397428, 39.90923],
          resizeEnable: true,
          zoom: 10
        })
        AMap.plugin(['AMap.ToolBar', 'AMap.Scale'], function () {
          map.addControl(new AMap.ToolBar())
          map.addControl(new AMap.Scale())
        })
      }
    }
  }
</script>
<style>
</style>
复制代码

效果如图:

 

原文地址:http://www.cnblogs.com/star-wind/p/6774204.html

转载于:https://www.cnblogs.com/karila/p/8139668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在Vue项目使用ArcGIS API for JavaScript调用ArcGIS服务器地图服务。首先,您需要将ArcGIS API添加到您的项目。 您可以使用以下命令安装ArcGIS API for JavaScript ``` npm install esri-loader ``` 然后在Vue组件加载ArcGIS API并使用它来调用地图服务。您可以使用以下代码示例作为参考: ```javascript <template> <div id="map"></div> </template> <script> import { loadModules } from 'esri-loader' export default { name: 'Map', data() { return { map: null, view: null } }, async created() { const [Map, MapView] = await loadModules([ 'esri/Map', 'esri/views/MapView' ]) this.map = new Map({ basemap: 'streets-navigation-vector' }) this.view = new MapView({ container: "map", map: this.map, center: [-118.80500, 34.02700], zoom: 13 }) const layer = { url: 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/Water_Network/MapServer' } const mapLayer = await loadModules(['esri/layers/MapImageLayer']) .then(([MapImageLayer]) => { return new MapImageLayer(layer) }) this.map.add(mapLayer) } } </script> ``` 该组件使用esri-loader库加载ArcGIS API for JavaScript,并在地图使用MapImageLayer来添加地图服务。请注意,此示例使用的是静态的地图服务。如果您需要使用动态地图服务,请使用DynamicMapLayer。 请注意,您需要使用合适的ArcGIS许可证才能使用ArcGIS API for JavaScript和ArcGIS服务器地图服务。同时,不要忘记在Vue项目包含ArcGIS API for JavaScript所需的样式表和资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值