I totally see the benefits of JSON, pull over a lightweight ar开发者_开发知识库ray of data to stuff into my Google Maps (and template through to show a list of the points as well). I've heard a lot of talk about KML.
Which is the preferable way to pull points into Google Maps, JSON || KML? My primary concern is performance, but also breadth of features that could potentially improve the map.
Personally I use json because the kml resulting features are less flexible than the features that you create from parsing json. For example, you can't add a hover event to a polygon from kml.
KML, being XML, is very heavyweight compared to JSON if you're just loading simpler data and will suffer performance penalties not only in network transfer but in parsing time as a result. That's the downside to JSON, of course, is that you're limited not only in expressive power potentially (depending on what the Google Maps components can consume via JSON versus KML), but also on which clients can consume your data. If you give your data in KML, anything that reads KML can use your data, like Google Earth. It's like the RSS of geographic data, that is if you want or need it to be.
These past few months I've done a lot of work with OpenLayers and have really enjoyed it. The library is open source and includes tons of formats out of the box including GeoJSON and KML. Here's an example demoing Google Maps integration. Here's the rest of the examples. Combined OpenLayers and ExtJS and it results in GeoExt (examples here).
I would like to make a case for KML, specially in the context of Google Maps:
- KML is an official standard curated by OGC;
- Google itself has created KML in the first place, and maintains an extensive documentation about it. It even has a library for KML;
- Google maps API has good support for KML Layers (although it also has good support for GeoJSON);
- A lot of other Google mapping applications (Google Earth, Google My Maps, Google Maps Routing) export KML natively, or offer to download/save the data as KML. That Import/Export functionality, as far as I know, is not so developed for GeoJSON, at least yet (as of 2016).
You can convert between KMZ, KML and GeoJSON:
KMZ is simply a ZIP archive of a KML file. So at least KMZ -> KML is easy.
# GeoJSON -> KML
$ ogr2ogr -f 'KML' -a_srs EPSG:4326 output.kml input.geojson
# KML -> GeoJSON
$ ogr2ogr -preserve_fid -f GeoJSON -a_srs EPSG:4326 output.geojson input.kml
And also
# KML -> GeoJSON
$ pip3 install kml2geojson --user
$ k2g input.kml output_directory
If upload speed is a concern, I would go with the compressed file. My guess: GeoJSON is way smaller for small data, KMZ is way smaller if your Features become more complex. But I'd be happy to see some benchmarks / numbers / examples.
精彩评论