Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Summary:</strong> You can't until <a href="http://code.google.com/p/gmaps-api-issues/issues/detail?id=2590" rel="nofollow">issue 2590</a> is fixed, which may be a while because Google have <a href="https://code.google.com/p/gmaps-api-issues/issues/detail?id=2590#c13" rel="nofollow">closed this issue as WontFix</a>. There are workarounds you can try to achieve the same end result, but as it stands you cannot simply upload a KML file using the Google Maps Data API.</p> <p><strong>Long version:</strong></p> <p>I <strike>don't</strike> didn't have any Python code to do this, but the <a href="http://code.google.com/apis/maps/documentation/mapsdata/" rel="nofollow">Google Maps Data API</a> allows you to do this with a series of HTTP requests. See <a href="http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#uploading_kml" rel="nofollow">Uploading KML</a> in the HTTP Protocol section of the <a href="http://code.google.com/apis/maps/documentation/mapsdata/developers_guide.html" rel="nofollow">Developers Guide</a> for the documentation on how to do this. So one possible Python solution would be to use something like <a href="http://docs.python.org/library/httplib.html" rel="nofollow">httplib</a> in the standard library to do the appropriate HTTP requests for you.</p> <p>After various edits and your feedback in the comments, here is a script that takes a Google username and password via the command line (be careful how you use it!) to obtain the <code>authorization_token</code> variable by making a <a href="http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#ClientLogin" rel="nofollow">ClientLogin authentication request</a>. With a valid username and password, the auth token can be used in the <code>Authorization</code> header for POSTing the KML data to the Maps Data API.</p> <pre><code>#!/usr/bin/env python import httplib import optparse import sys import urllib class GoogleMaps(object): source = "daybarr.com-kmluploader-0.1" def __init__(self, email, passwd): self.email = email self.passwd = passwd self._conn = None self._auth_token = None def _get_connection(self): if not self._auth_token: conn = httplib.HTTPSConnection("www.google.com") params = urllib.urlencode({ "accountType": "HOSTED_OR_GOOGLE", "Email": self.email, "Passwd": self.passwd, "service": "local", "source": self.source, }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", } conn.request("POST", "/accounts/ClientLogin", params, headers) response = conn.getresponse() if response.status != 200: raise Exception("Failed to login: %s %s" % ( response.status, response.reason)) body = response.read() for line in body.splitlines(): if line.startswith("Auth="): self._auth_token = line[5:] break if not self._auth_token: raise Exception("Cannot find auth token in response %s" % body) if not self._conn: self._conn = httplib.HTTPConnection("maps.google.com") return self._conn connection = property(_get_connection) def upload(self, kml_data): conn = self.connection headers = { "GData-Version": "2.0", "Authorization": 'GoogleLogin auth=%s' % (self._auth_token,), "Content-Type": "application/vnd.google-earth.kml+xml", } conn.request("POST", "/maps/feeds/maps/default/full", kml_data, headers) response = conn.getresponse() if response.status != 200: raise Exception("Failed to upload kml: %s %s" % ( response.status, response.reason)) return response.read() if __name__ == "__main__": parser = optparse.OptionParser() parser.add_option("-e", "--email", help="Email address for login") parser.add_option("-p", "--passwd", help="Password for login") options, args = parser.parse_args() if not (options.email and options.passwd): parser.error("email and passwd required") if args: kml_file = open(args[0], "r") else: kml_file = sys.stdin maps = GoogleMaps(options.email, options.passwd) print maps.upload(kml_file.read()) </code></pre> <p><strong>Unfortunately</strong>, even when using valid login credentials to obtain a valid authorization token and using a valid KML file containing exactly the example as given in the documentation, the API responds to the KML post with a <code>400 Bad Request</code>. Apparently this is a known issue (<a href="http://code.google.com/p/gmaps-api-issues/issues/detail?id=2590" rel="nofollow">2590</a> reported July 22nd 2010) so please vote for and comment on that if you'd like Google to fix.</p> <p>In the meantime, without that bug fixed, you could try</p> <ol> <li>Create the map without uploading KML, and then upload KML features as appropriate, as suggested in <a href="http://code.google.com/p/gmaps-api-issues/issues/detail?id=2590#c9" rel="nofollow">comment #9 on the issue</a> from Google, when they confirmed that the bug exists.</li> <li><a href="http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#uploading_xml" rel="nofollow">uploading XML</a> or <a href="http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#uploading_csv" rel="nofollow">uploading CSV</a> instead of KML if these methods support what you need to get done</li> <li>fiddling with the format of your KML data. <a href="http://groups.google.com/group/google-maps-data-api/msg/c34efa7fef259e0f" rel="nofollow">This post</a> in the Google Group for the API suggests that this might help, but it looks complicated.</li> </ol> <p>Good luck</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload