Created
November 27, 2013 21:31
-
-
Save cyrildiagne/7683618 to your computer and use it in GitHub Desktop.
zoom/pan animation test with google map js api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(-> | |
| address_from = params['from'] | |
| address_to = params['to'] | |
| map = null | |
| latlngFrom = null | |
| latlngTo = null | |
| animLatLng = null | |
| currStatus = 'zoomOut' | |
| intervalId = -1 | |
| zoomDelay = params['zoomDelay'] or 500 | |
| zoomMin = params['zoomMin'] or 12 | |
| zoomMax = params['zoomMax'] or 20 | |
| loadGeocode = (address) -> | |
| geocoder = new google.maps.Geocoder() | |
| geocoder.geocode { address: decodeURIComponent(address)}, geocodeLoaded | |
| geocodeLoaded = (results, status) -> | |
| if status == google.maps.GeocoderStatus.OK | |
| if latlngFrom | |
| latlngTo = results[0].geometry.location | |
| else | |
| latlngFrom = results[0].geometry.location | |
| loadGeocode address_to | |
| else | |
| window.location.href = window.location.href.split('?')[0] + '?err=address_not_found' | |
| if latlngFrom and latlngTo then initMap() | |
| initMap = -> | |
| mapOptions = | |
| center: latlngFrom | |
| mapTypeId: google.maps.MapTypeId.SATELLITE | |
| disableDefaultUI: true | |
| draggable: false | |
| zoom: zoomMax | |
| map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions) | |
| intervalId = setInterval update, zoomDelay | |
| setStatus = (status, interval) -> | |
| clearInterval intervalId | |
| currStatus = status | |
| intervalId = setInterval update, interval | |
| update = -> | |
| if currStatus is 'zoomOut' | |
| zoom = map.getZoom() - 1 | |
| map.setZoom zoom | |
| if zoom <= zoomMin | |
| animLatLng = latlngFrom | |
| setStatus( 'pan', 30 ) | |
| else if currStatus is 'pan' | |
| lat = animLatLng.lat() | |
| delta_lat = (latlngTo.lat() - lat) * 0.05 | |
| lng = animLatLng.lng() | |
| delta_lng = (latlngTo.lng() - lng) * 0.05 | |
| if Math.abs(delta_lat)<0.00001 or Math.abs(delta_lng)<0.00001 | |
| animLatLng = latlngTo | |
| setStatus 'zoomIn', zoomDelay | |
| else | |
| animLatLng = new google.maps.LatLng( lat + delta_lat, lng + delta_lng ) | |
| map.panTo animLatLng | |
| else if currStatus is 'zoomIn' | |
| zoom = map.getZoom() + 1 | |
| map.setZoom zoom | |
| if zoom >= zoomMax | |
| clearInterval intervalId | |
| $('<div>').attr('id', 'map-canvas').appendTo( $('body') ) | |
| google.maps.event.addDomListener window, 'load', -> | |
| loadGeocode address_from | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment