Slippy maps for gmaps4rails

情到浓时终转凉″ 提交于 2019-12-02 09:35:01

问题


I thought it would be nice to use OpenStreetMap (or OpenCycleMap) as a map tile provider within the google API. Here is an example of how this is done in plain javascript:

<script type="text/javascript">
        var element = document.getElementById("map");

        /*
        Build list of map types.
        You can also use var mapTypeIds = ["roadmap", "satellite", "hybrid", "terrain", "OSM"]
        but static lists sucks when google updates the default list of map types.
        */
        var mapTypeIds = [];
        for(var type in google.maps.MapTypeId) {
            mapTypeIds.push(google.maps.MapTypeId[type]);
        }
        mapTypeIds.push("OSM");

        var map = new google.maps.Map(element, {
            center: new google.maps.LatLng(48.1391265, 11.580186300000037),
            zoom: 11,
            mapTypeId: "OSM",
            mapTypeControlOptions: {
                mapTypeIds: mapTypeIds
            }
        });

        map.mapTypes.set("OSM", new google.maps.ImageMapType({
            getTileUrl: function(coord, zoom) {
                return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
            },
            tileSize: new google.maps.Size(256, 256),
            name: "OpenStreetMap",
            maxZoom: 18
        }));
    </script>

http://wiki.openstreetmap.org/wiki/Google_Maps_Example

This way one could have an OSM map (which looks a lot nicer for example for hiking or cycling purposes) combined with the google map types and the much nicer google interface, plus all of the methods and options that are only available for google maps in gmaps4rails (eg. clustering etc.).

I have tried to figure out how to implement this but it is frankly a bit over my head.

Has anybody else tried to achieve something similar?

Edit: I have figured out a way to achieve what I want. It's not pretty but I thought I should post it anyway, so maybe others can come up with better ideas.

In gmaps4rails.base.js.coffee I have added this method:

create_OSM : ->
OSMMapTypeOptions = new google.maps.ImageMapType(
  getTileUrl: (coord, zoom) ->
    "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png"
  tileSize: new google.maps.Size(256, 256)
  name: "OSM"
  maxZoom: 18
)
@serviceObject.mapTypes.set("OSM", OSMMapTypeOptions)
CycleMapTypeOptions = new google.maps.ImageMapType(
  getTileUrl: (coord, zoom) ->
    "http://tile.opencyclemap.org/cycle/" + zoom + "/" + coord.x + "/" + coord.y + ".png"
  tileSize: new google.maps.Size(256, 256)
  name: "Cycle"
  maxZoom: 18
)
@serviceObject.mapTypes.set("OCM", CycleMapTypeOptions)
@serviceObject.setMapTypeId("OSM")

In js_builder.rb I added the method call like this @js << "#{gmap_id}.create_OSM();" just after @js << "#{gmap_id}.initialize();".

Lastly, my view code looks like this:

 <%= gmaps("markers" => {"data" => @json},
                "map_options" => {"type" => "TERRAIN", :raw => '{mapTypeControlOptions: {mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, "OSM", "OCM"], style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}}'}) %>

It works and I am pleased with the result. Needs some serious cleaning up and refactoring though. Any commments?

来源:https://stackoverflow.com/questions/11404725/slippy-maps-for-gmaps4rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!