0

My Google map code

var myOptions = {
    center: {lat: 55.864237, lng: -4.251806},
    zoom: 16,
    mapTypeId: google.maps.MapTypeId.SATELLITE
};

var map1 = new google.maps.Map(document.getElementById("map"), myOptions);

var geoXml = new geoXML3.parser({
    map: map1
});
var source = 'https://developers.google.com/kml/documentation/KML_Samples.kml';

geoXml.parse(source);

And the error i am getting is as follows:

Failed to load https://developers.google.com/kml/documentation/KML_Samples.kml: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

XML parse error Unable to retrieve https://developers.google.com/kml/documentation/KML_Samples.kml

Anyone knows how can i solve this issue?

Paul
  • 23
  • 1
  • 6
  • 1
    Try downloading the kml file and loading it locally into your page. The error you are getting is due to a security restriction where google is blocking you from loading resources that reside on their server. [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) – Tom Sep 09 '18 at 14:43

1 Answers1

0

GeoXml3 uses the browser's XmlHttpRequest object, which is subject to the same origin policy. You can't accesshttps://developers.google.com from http://localhost:8080 (different origins).

To use GeoXml3, either copy that file to your server and access it through a relative URL or access it through a proxy on http://localhost:8080.

example loading that URL through a proxy

example using the proxy property of GeoXml3

Another option would be to use the Google Maps Javascript API v3 KmlLayer, which accesses the KML from Google's servers and isn't subject to the same origin policy.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Actually I tried KML Layer at first and stuck up in following issue. 1. I have uploded my kml file on dropbox. And i successfully render that kml file in google map. But color schema and polygons with different colors were not displaying in map. It was just showing polylines. I came to know this issue when i uploaded this file on online KML viewer.(http://ivanrublev.me/kml/). Link of my kml - https://www.dropbox.com/s/5fj8cns3nglcrcn/TRACKMAP.kml?dl=1 Thats why i was trying by another way. – Paul Sep 09 '18 at 18:20
  • The KML is not "mine", it is from Google (in the OP's question). The polygons in the examples are filled with color. Look closer at the style definitions. – geocodezip Sep 10 '18 at 17:53