vue中vue2-google-maps使用谷歌地图的基础操作
小哥我最近使用谷歌地图做了一个项目,于是乎各种坑就扑面而来,未免下次接着踩坑特留下自己的爬坑记录。
首先我是没用过谷歌地图也不知道靠谱不靠谱,于是乎傻傻的入坑了,
1.首先你要是没有vpn(或者fq工具)那可能就寸步难行,赶快申请一个吧,用坚果或者蓝*都可以(如果没有,地图压根就不显示)
2.调用谷歌地图逆编码接口(你会发现你直接放在浏览器上打开,数据显示都挺好,可是自己用接口怎么调用都不行,恭喜你踩了第二个坑)如下:
谷歌官方API: https://maps.google.com/maps/api/geocode/json?address=New York&sensor=true_or_false&key=谷歌的key(浏览器直接打开没毛病啊,但是接口你怎么都掉不通,不信你试试)
国内API:https://ditu.google.cn/maps/api/geocode/json?address=New York&key=谷歌的key;(小伙子,你没看错这个接口让后台直接调用,然后把数据返还给你就行,我的用的是php,代码图片给你放出来,仅供参考,如需php代码可联系我,算了还是别联系我了,我代码也放下边吧)
如果(后台兄弟不配合,那兄弟晚上出去吃个烧烤也许就解决了,一顿不行来两顿),如果还不行,小伙子我也帮不了你了。。。。。
php代码如下:
$ipAll = $this->input->get('ipAll');
// $result ="";
// $url = 'http://api.map.baidu.com/geocoding/v3/?address='.$ipAll.'&output=json&ak=X7UHQqYy3WobTXHk3Mw3oN96ahHcQuuG&callback=showLocation';
// $abc= file_get_contents($url);
$prepAddr = str_replace(' ','+',$ipAll);
$url = 'https://ditu.google.cn/maps/api/geocode/json?address='.$prepAddr.'&key=AIzaSyB9W2V3_GT8_tKfO-PA6kT2_eeW35yUgv8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $this->json($output, $this::SUCCESS, $message = $this->lang->line('text_resp_success'));
上面扯淡完了,下面就到了前端的了,这里呢我想说没有还是需要蓝灯和坚果的支援,主要是访问的时候,
1.生成vue文件就不说了,同时需要安装谷歌地图使用vue2-google-maps
vue2-google-maps 官网地址(https://www.npmjs.com/package/vue2-google-maps)
npm install vue2-google-maps
然后在min.js中引入
import Vue from 'vue'
import * as VueGoogleMaps from 'vue2-google-maps'
Vue.use(VueGoogleMaps, {
load: {
key: 'YOUR_API_TOKEN',
libraries: 'places', // This is required if you use the Autocomplete plugin
// OR: libraries: 'places,drawing'
// OR: libraries: 'places,drawing,visualization'
// (as you require)
//// If you want to set the version, you can do so:
// v: '3.26',
},
//// If you intend to programmatically custom event listener code
//// (e.g. `this.$refs.gmap.$on('zoom_changed', someFunc)`)
//// instead of going through Vue templates (e.g. `<GmapMap @zoom_changed="someFunc">`)
//// you might need to turn this on.
// autobindAllEvents: false,
//// If you want to manually install components, e.g.
//// import {GmapMarker} from 'vue2-google-maps/src/components/marker'
//// Vue.component('GmapMarker', GmapMarker)
//// then disable the following:
// installComponents: true,
})
下面就是地图的代码了,别着急(如下完整代码,我想你可能想偷懒于是乎我git上给你留了一份,但是需要你在min.js中放入自己申请的谷歌地图的key地址如下:)
<template>
<div>
<gmap-map
:center="centers"
:zoom="11"
map-type-id="terrain"
style="width: 100%; height: 340px"
>
<gmap-marker
@dragend="updateMaker"
:key="index"
v-for="(m, index) in markers"
:position="m.position"
:clickable="true"
:draggable="true"
@click="centers=m.position"
></gmap-marker>
<!-- @click="centers=m.position" -->
</gmap-map>
</div>
</template>
<script>
export default {
data() {
return {
centers: {lat: 39.90419989999999,lng: 116.4073963},
markers: [{
position: {lat: 39.90419989999999,lng: 116.4073963}
}],
place: null,
}
},
description: 'Autocomplete Example (#164)',
mounted() {
},
methods: {
setPlace(place) {
this.place = place
},
setDescription(description) {
this.description = description;
},
usePlace(place) {
if (this.place) {
var newPostion = {
lat: this.place.geometry.location.lat(),
lng: this.place.geometry.location.lng(),
};
this.center = newPostion;
this.markers[0].position = newPostion;
this.place = null;
}
},
updateMaker: function(event) {
console.log('updateMaker, ', event.latLng.lat());
this.markers[0].position = {
lat: event.latLng.lat(),
lng: event.latLng.lng(),
}
},
}
}
</script>
国内想要使用谷歌地图,需要将经纬度反向编码,在通过后台返回
国内,如下
国外如下(需要fq或者开vpn才可以访问)
https://maps.googleapis.com/maps/api/geocode/json?address=北京天安门&key=AIzaSyB9W2V3_GT8_tKfO-PA6kT2_eeW35yUgv8
是例子
https://blog.csdn.net/cc_1209/article/details/89416936
vue中vue2-google-maps使用谷歌地图的基础操作的更多相关文章
- Google Map Api 谷歌地图接口整理
一:基本知识: 1. 使用谷歌地图 API 的第一步就是要注册一个 API 密钥,需要注重一下两点: 1.假如使用 API 的页面还没有发布,只是在本地调试,可以不用密钥,随便用个字符串代替就可以了. ...
- Google Maps API显示地图的小示例
来源:http://www.ido321.com/1089.html 效果(新版Firefox中测试): 代码: <!DOCTYPE> <html> <head> ...
- Google Maps瓦片(tile)地图文件下载(1-11层级)
整理硬盘时,发现一份去年下载的谷歌地图瓦片文件,整理并分享给大家. 地图来源:Google Maps(应该是国内谷歌地图) 采集时间:2017年6月 采集范围:0-6层级世界范围:7-11层级中国范围 ...
- 利用 Google API 调用谷歌地图 演示1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android Google Maps 监听地图缩放
接上篇.http://www.cnblogs.com/maomishen/p/3556297.html 由于公司项目要求,需要对google map监听地图的缩放(zoom)来进行一些操作. 但是在网 ...
- how to add borders for a google map marker 谷歌地图 自己定义图钉
If you are not satisfied with default Google map Marker (Default google marker can only be a icon, i ...
- vue中使用echarts来绘制中国地图,NuxtJS制作疫情地图,内有详细注释,我就懒得解释了,vue cli制作疫情地图 代码略有不同哦~~~
我的代码自我感觉----注释一向十分详细,就不用过多解释都是什么了~~ 因为最近疫情期间在家实在是没事干,想找点事,就练手了个小demo 首先上 NuxtJs版本代码,这里面 export defau ...
- Android系统Google Maps开发实例浅析
Google Map(谷歌地图)是Google公司提供的电子地图服务.包括了三种视图:矢量地图.卫星图片.地形地图.对于Android系统来说,可以利用Google提供的地图服务来开发自己的一些应用. ...
- Google Maps API Web Services
原文:Google Maps API Web Services 摘自:https://developers.google.com/maps/documentation/webservices/ Goo ...
- Google Maps API v2密钥申请
1. 进入到Google APIs Console页面 https://code.google.com/apis/console/ 点击左边导航栏的Seivices进入 在All services 的 ...
随机推荐
- Springcloud源码学习笔记1—— Zuul网关原理
系列文章目录和关于我 源码基于 spring-cloud-netflix-zuul-2.2.6.RELEASE.jar 需要具备SpringMVC源码功底 推荐学习https://www.cnblog ...
- 全志V3S 调试串口更改或关闭
有时项目外设比较多,很容易造成串口不够用的情况. 最近就遇到了,新增加一个GPS模块串口的,串口现在外部只有原来的调试串口可以用,所以 尝试将调试口更改为普通串口. 经过网上看大神们的文章和自己摸索, ...
- 微信小程序技巧-让特定组件首页始终展示修改编译条件即可,不用改json
- jjq(友链:https://tg.hszxoj.com/user/475)
- LeetCode_387. 字符串中的第一个唯一字符
写在前面 原文地址:https://leetcode.cn/problems/first-unique-character-in-a-string/ 难度:简单 题目 给定一个字符串 s ,找到 它的 ...
- 数据同步gossip协议原理与应用场景介绍
作者:京东物流 冯鸿儒 1 简介 Gossip是一种p2p的分布式协议.它的核心是在去中心化结构下,通过将信息部分传递,达到全集群的状态信息传播,传播的时间收敛在O(Log(N))以内,其中N是节点的 ...
- 【Vue】计算属性 监听属性 组件通信 动态组件 插槽 vue-cli脚手架
目录 昨日回顾 1 计算属性 插值语法+函数 使用计算属性 计算属性重写过滤案例 2 监听属性 3 组件介绍和定义 组件之间数据隔离 4 组件通信 父子通信之父传子 父子通信之子传父 ref属性 扩展 ...
- 安装、小demo、基本的步骤
FastAPI 有两个依赖支持: Starlette负责网络 Pydantic负责数据 安装: 安装命令 pip install fastapi FastAPI 还需要ASGI服务器,生产环境下可 ...
- Vladik and fractions
题目大意 给出 \(n\),求一组 \(x,y,z\) 满足 \(\frac 1x + \frac 1y + \frac 1z = \frac 2n\) 若不存在合法的解,输出 \(-1\) 其中 \ ...
- [POI2011]MET-Meteors 解题报告
语言系统紊乱了 QAQ 这道题感觉不是很难鸭 qwq. 先只考虑一个国家,怎么做?很显然,就直接二分一下就行了.判定答案可以维护一个差分数组,然后最后对它做一个前缀和,再求一下这个国家的流行数量就好了 ...