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 的 ...
随机推荐
- Java进阶篇——设计模式
设计模式 一.代理模式 使用代理类对真实对象进行代理,包括真实对象方法的调用.功能的扩展等.访问的时候也只能访问到代理对象,既保护了真实对象同时可以在原始对象上进行扩展.类似于中介在卖家和买家之间的角 ...
- .Net Core Logging模块源码阅读
.Net Core Logging模块源码阅读 前言 在Asp.Net Core Webapi项目中经常会用到ILogger,于是在空闲的时候就clone了一下官方的源码库下来研究,这里记录一下. 官 ...
- [Leetcode]旋转链表
题目 代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Li ...
- 算法之Floyd-Warshall算法【c++】【图论】【最短路】
我们作为刚学图论的小蒟蒻,先接触到的算法一定是图上最短路径算法.而最短路算法中最简单的当属Floyd-Warshall算法.下面是一些基本介绍: 该算法可以计算图上任意两点间的最短路径 时间复杂度: ...
- 从 GPT2 到 Stable Diffusion:Elixir 社区迎来了 Hugging Face
上周,Elixir 社区向大家宣布,Elixir 语言社区新增从 GPT2 到 Stable Diffusion 的一系列神经网络模型.这些模型得以实现归功于刚刚发布的 Bumblebee 库.Bum ...
- 单实例Primary快速搭建Standby RAC参考手册(19.16 ADG)
环境:Single Instance -> RAC Single Instance: db_name=demo db_unique_name=demo instance_name=demo se ...
- 【随笔记】XR872 Codec 驱动移植和应用程序实例(附芯片调试方法)
XR872 的 SDK 是我目前接触过那么多款 MCU 的 SDK 中,唯一一个将框架和 RTOS 结合的非常完美的 SDK .无论是代码风格还是框架的设计,看起来都很赏心悦目,而且是源码开源.希望能 ...
- day05-SpringMVC底层机制简单实现-01
SpringMVC底层机制简单实现-01 主要完成:核心分发控制器+Controller和Service注入容器+对象自动装配+控制器方法获取参数+视图解析+返回JSON格式数据 1.搭建开发环境 创 ...
- Sentry 后端云原生中间件实践 ClickHouse PaaS ,为 Snuba 事件分析引擎提供动力
目录(脑图) ClickHouse PaaS 云原生多租户平台(Altinity.Cloud) 官网:https://altinity.cloud PaaS 架构概览 设计一个拥有云原生编排能力.支持 ...
- 元数据库 information_schema.tables
转 https://www.cnblogs.com/ssslinppp/p/6178636.html 1.information_schema数据库 对于mysql和Infobright等数据库,i ...