Extract OSM tile setup to own directive

This commit is contained in:
Rohan Mitchell
2016-06-30 12:14:47 +10:00
parent 54028f4e7e
commit f586dbc3e1
4 changed files with 23 additions and 18 deletions

View File

@@ -0,0 +1,21 @@
Darkswarm.directive 'mapOsmTiles', ($timeout) ->
restrict: 'E'
require: '^googleMap'
scope: {}
link: (scope, elem, attrs, ctrl) ->
$timeout =>
map = ctrl.getMap()
map.mapTypes.set 'OSM', new google.maps.ImageMapType
getTileUrl: (coord, zoom) ->
# "Wrap" x (logitude) at 180th meridian properly
# NB: Don't touch coord.x because coord param is by reference, and changing its x property breaks something in Google's lib
tilesPerGlobe = 1 << zoom
x = coord.x % tilesPerGlobe
if x < 0
x = tilesPerGlobe + x
# Wrap y (latitude) in a like manner if you want to enable vertical infinite scroll
'http://tile.openstreetmap.org/' + zoom + '/' + x + '/' + coord.y + '.png'
tileSize: new (google.maps.Size)(256, 256)
name: 'OpenStreetMap'
maxZoom: 18

View File

@@ -9,29 +9,11 @@ Darkswarm.directive 'mapSearch', ($timeout) ->
$timeout =>
map = ctrl.getMap()
# Does this *really* belong here? It's not about search.
scope.useOsmTiles map
searchBox = scope.createSearchBox map
scope.respondToSearch map, searchBox
scope.biasResults map, searchBox
scope.useOsmTiles = (map) ->
map.mapTypes.set 'OSM', new google.maps.ImageMapType
getTileUrl: (coord, zoom) ->
# "Wrap" x (logitude) at 180th meridian properly
# NB: Don't touch coord.x because coord param is by reference, and changing its x property breaks something in Google's lib
tilesPerGlobe = 1 << zoom
x = coord.x % tilesPerGlobe
if x < 0
x = tilesPerGlobe + x
# Wrap y (latitude) in a like manner if you want to enable vertical infinite scroll
'http://tile.openstreetmap.org/' + zoom + '/' + x + '/' + coord.y + '.png'
tileSize: new (google.maps.Size)(256, 256)
name: 'OpenStreetMap'
maxZoom: 18
scope.createSearchBox = (map) ->
input = document.getElementById("pac-input")
map.controls[google.maps.ControlPosition.TOP_LEFT].push input