Google Places API cors

2020-03-31 03:52发布

I'm trying to do a get request from my localhost machine(And my app will stay on localhost) to get some information about a place.

However, the Google Places API and/or my Chrome won't let me do this request due to CORS (XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/details/json?placeid=[placeid]&key=[key]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.).

Is there some way (Without a CORS Chrome plugin) to get access from the Google Places API?

I'm just using a simple GET request in Javascript with Vue2 & Vue Resource:

this.$http.get(`https://maps.googleapis.com/maps/api/place/details/json?placeid=${googleplacesPlace}&key=${googleplacesToken}`).then(response => {
})

3条回答
Lonely孤独者°
2楼-- · 2020-03-31 04:22
  1. Create an account on Google Maps Platform
  2. Open Vue Project
  3. Make a js file (src/utils/gmap.js)
// src/utils/gmaps.js
const API_KEY = 'XXXXXYOURAPIKEYXXXX';
const CALLBACK_NAME = 'gmapsCallback';

let initialized = !!window.google;
let resolveInitPromise;
let rejectInitPromise;
const initPromise = new Promise((resolve, reject) => {
  resolveInitPromise = resolve;
  rejectInitPromise = reject;
});

export default function init() {
  if (initialized) return initPromise;

  initialized = true;
  window[CALLBACK_NAME] = () => resolveInitPromise(window.google);

  const script = document.createElement('script');
  script.async = true;
  script.defer = true;
  script.src = `https://maps.googleapis.com/maps/api/js?key=${API_KEY}&callback=${CALLBACK_NAME}`;
  script.onerror = rejectInitPromise;
  document.querySelector('head').appendChild(script);

  return initPromise;
}
  1. In view js (src/views/MyMap.vue)
<template>
  <div class="my-map">
      My Map
      <div id="map"></div>
  </div>
</template>

<script>
import gmapsInit from '@/utils/gmaps';
export default {
  name: 'myMap',
  components: {
  },
  data () {
    return {
    }
  },
  computed: {
  },
  async mounted() {
    try {
      const google = await gmapsInit();
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
    });

    } catch (error) {
      console.error(error);
    }
  },
  methods:{
  }

}
</script>

<style>
#map{
    width:400px;
    height:400px;
}
</style>

Ref on

Using the Google Maps API with Vue.js

Maps JavaScript API, Hello World

查看更多
放我归山
3楼-- · 2020-03-31 04:31

Same issue for me, solved it with the Google Maps Javascript API this way :Instead of using the direct api url with does not accept CORS, use instene of google map (new google.maps.places...)

 var request = {
  location: new google.maps.LatLng(lat, lng), // gogle map latLng objet
  radius: '5000',
  types: ['airport']
};

let service = new google.maps.places.PlacesService(this.map);
service.nearbySearch(request, this.Fncallback);
  }

 Fncallback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      var place = results[i];
  }
 }
}
查看更多
老娘就宠你
4楼-- · 2020-03-31 04:35
const PROXY_URL = 'https://cors-anywhere.herokuapp.com/';
const TARGET_URL = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?&key=KEY'
const URL = PROXY_URL + TARGET_URL

you can use this to bypass cors.

查看更多
登录 后发表回答