简体   繁体   English

尝试使用Angular 2从localhost访问街景时的Google街景API API问题

[英]Google Street View API CORS Issue when trying to access street view from localhost using Angular 2

I am getting the following error when trying to access the panoramas . 尝试访问全景图时出现以下错误。 I come across this error randomly . 我随机遇到了这个错误。 Like sometimes it happens and other times it doesn't can someone please elaborate how can I resolve this . 就像有时它会发生,有时它不会有人请详细说明如何解决这个问题。

Error : Access to Image at ' https://lh3.ggpht.com/-GEWinvnWrN8/VgoOwiNcv2I/AAAAAAAAA3w/vhFJX1VOvr4fDNV2JmKDPhUUx08MeBqdwCJkC/x0-y0-z0/p ' from origin 'localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 错误:来自'localhost:4200'的' https://lh3.ggpht.com/-GEWinvnWrN8/VgoOwiNcv2I/AAAAAAAAA3w/vhFJX1VOvr4fDNV2JmKDPhUUx08MeBqdwCJkC/x0-y0-z0/p '访问图像已被CORS政策阻止:否' Access-Control-Allow-Origin'标头出现在请求的资源上。 Origin 'localhost:4200' is therefore not allowed access. 因此不允许原点'localhost:4200'访问。 The response had HTTP status code 403. 响应具有HTTP状态代码403。

I got this CORS error while trying to access an API that I built using DRF too . 尝试访问我使用DRF构建的API时,我收到此CORS错误。 But later I solved it using a middleware at my API. 但后来我在我的API上使用中间件解决了它。 It doesn't sound logical to me that Google is not allowing me to access the images if it's not on same domain . 如果Google不在同一个域中,那么谷歌不允许我访问这些图像对我来说听起来不合逻辑。 I am working on Angular 2. this is the code that I call when user enters x=lat and y=lon . 我正在研究Angular 2.这是我在用户输入x = lat和y = lon时调用的代码。

    var map = new google.maps.Map(document.getElementById('map'), {
             zoom: 15,
             center: {lat: this.x, lng: this.y}
           });
    var panorama = new google.maps.StreetViewPanorama(
                 document.getElementById('pano'), {
                   position:  {lat: this.x, lng: this.y},
                   pov: {
                     heading: 34,
                     pitch: 10
                   }
                 });
    var cafeMarker = new google.maps.Marker({
                       position: {lat: this.x, lng: this.y},
                       map: map,
                       icon: 'https://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
                       title: 'Canteen'
                   });
                   panorama.addListener('position_changed', function() {
                 console.log(panorama.getPosition().lat() +" " + panorama.getPosition().lon());
            });

map.setStreetView(panorama);`

Check your reference to google API in index.html It should be: 在index.html中检查您对google API的引用应该是:

src="https://maps.google.com/maps/api/js?key=YOUR-KEY">

(notice the http S ) (注意http S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM