The Alpha
The Alpha

Reputation: 146191

How to create a google map with kml layer overlay dynamically

function initialize() {
    var myOptions = {
      center: new google.maps.LatLng(23.71181,90.407467),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    var map_coverage = new google.maps.KmlLayer('http://mydomain.com/myfile.kmz'  );
    map_coverage.setMap(map);       
    map.setZoom(12);
    map.setCenter(dhaka_latlng);    
  }

Above code outputs the following map with kmllayer and the kml file has been created with Google earth and saved as a kmz (compressed) file on the server. This file contains some xml data and the colored area represents some coordinates in a tag along with other tags like

<coordinates>
    90.27401489730001,23.9502580159,0 90.2740186914,23.95007761100001 ... so on
</coordinates>

I want to dynamically create a similar map with kmllayer using javascript api-v3 but without help of Google earth. I mean i want to create kml file using php and save it on the server as somefile.kmz. Is that possible ? Any kinds of help will be appreciated. Thanks !

I can post the original kml file if you need but it's to large

enter image description here

Upvotes: 1

Views: 9198

Answers (1)

miguev
miguev

Reputation: 4840

Yes, it is possible to generate a new KML file server-side (PHP) and then tell JavaScript to load a new KmlLayer using the URL that will generate the KML content. Note that the KML URL:

  • must be publicly accessible, since Google servers need to download it
  • will be cached by Google servers, serving a different content at the same URL won't refresh it

To overcome this caching, just add some random clatter to the URL. However, be aware caching is there to make it fast. You can't have both "super-fast KML" and "real-time fresh data" at once. Also, keep an eye on your own server's response times for KML URLs, the faster the better.

Upvotes: 1

Related Questions