Files
openfoodnetwork/app/assets/javascripts/darkswarm/services/gmaps_geo.js.erb.coffee
Matt-Yorkley 78fc6b7138 Rename "Geo" service to "GmapsGeo" for clarity.
This service directly calls methods in the google maps javascript included in the darkswarm layout. If we're moving away from it, we should clarify where it's used.
2020-06-05 08:35:15 +02:00

23 lines
862 B
CoffeeScript

Darkswarm.service "GmapsGeo", ->
new class GmapsGeo
OK: google?.maps?.GeocoderStatus?.OK
# Usage:
# Geo.geocode address, (results, status) ->
# if status == Geo.OK
# console.log results[0].geometry.location
# else
# console.log "Error: #{status}"
geocode: (address, callback) ->
geocoder = new google.maps.Geocoder()
geocoder.geocode {'address': address, 'region': "<%= Spree::Country.find_by_id(Spree::Config[:default_country_id]).iso %>"}, callback
distanceBetween: (src, dst) ->
google.maps.geometry.spherical.computeDistanceBetween @toLatLng(src), @toLatLng(dst)
# Wrap an object in a google.maps.LatLng if it has not been already
toLatLng: (locatable) ->
if locatable.lat?
locatable
else
new google.maps.LatLng locatable.latitude, locatable.longitude