"use strict"; /* global google: true */ /** * Map adapter for Google Maps API V3 * Refer to map-adapter-template.js for detailed JSDoc */ (function () { var adapter = {}; // Load Google Maps keys and depended config adapter.init = function (data, deferred) { // define the callback func for the google map api link window.loadGoogleMapsDependedScripts = function () { // load map depended scripts $.getJSON('scripts/vendor/google-maps-depended-scripts.json', function (scripts) { bmcMaps.adapters.loadScripts(scripts).always(function () { deferred.resolve(); }); }); }; // before loading Google Maps, check if Google Maps API keys are present... if (data.GOOGLE_MAPS_API_KEY || data.GOOGLE_MAPS_API_CLIENT_ID) { // have to append the callback param here, otherwise will report document can't be writen by async-loaded external script error var googleMapsApiLink = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=loadGoogleMapsDependedScripts'; if (data.GOOGLE_MAPS_API_CLIENT_ID) { googleMapsApiLink += '&client=' + data.GOOGLE_MAPS_API_CLIENT_ID; } else if (data.GOOGLE_MAPS_API_KEY) { googleMapsApiLink += '&key=' + data.GOOGLE_MAPS_API_KEY; } // try to load Google Maps, and set corresponding availability flag $.getScript(googleMapsApiLink) .done(function () { bmcMaps.adapters.saveMapsAvailability(true); }) .fail(function () { bmcMaps.adapters.saveMapsAvailability(false); deferred.resolve(); }); } else { // otherwise, set a flag about Google Maps not available bmcMaps.adapters.saveMapsAvailability(false); // and go straight to next step deferred.resolve(); } }; adapter.event = {}; adapter.event.addListener = function (abstraction, eventType, listener) { google.maps.event.addListener(abstraction.impl, eventType, listener); }; adapter.event.clearInstanceListeners = function (abstraction) { google.maps.event.clearInstanceListeners(abstraction.impl); }; adapter.event.trigger = function (abstraction, eventType) { google.maps.event.trigger(abstraction.impl, eventType); }; adapter.event.addDomListener = function (element, eventType, listener) { google.maps.event.addDomListener(element.impl ? element.impl : element, eventType, listener); }; adapter.event.addListenerOnce = function (abstraction, eventType, listener) { google.maps.event.addListenerOnce(abstraction.impl, eventType, listener); }; adapter.MapTypeRegistry = function (map) { this.impl = map.impl.mapTypes; }; adapter.MapTypeRegistry.prototype.set = function (id, mapType) { if (mapType.tileSize) { mapType.tileSize = new google.maps.Size(mapType.tileSize.width, mapType.tileSize.height); } this.impl.set(id, mapType); }; adapter.geocoder = {}; adapter.geocoder.geocode = function (address, successCb, failCB) { var geocoder = new google.maps.Geocoder(); geocoder.geocode({ address: address }, function (results, status) { if (status === google.maps.GeocoderStatus.OK) { var location = results[0].geometry.location; var latLng = new bmcMaps.LatLng(location.lat(), location.lng()); successCb(latLng); } else { failCB(); } }); }; adapter.getDirectionUrl = function (address) { return '//maps.google.com/maps?saddr=Current+Location&daddr=' + address; }; adapter.map = function (element, options) { return new adapter.Map(element, options); }; adapter.Map = function (element, options) { if (options) { this.mapTypeId = options.mapTypeId; if (options.center) { options.center = new google.maps.LatLng(options.center._lat, options.center._lng); } } this.impl = new google.maps.Map(element, options); }; adapter.Map.prototype.addMarker = function (location, icon) { var locationMarker = new google.maps.Marker({ position: new google.maps.LatLng(location._lat, location._lng), map: this.impl, icon: icon }); return locationMarker; }; adapter.Map.prototype.getMapTypeRegistry = function () { return new adapter.MapTypeRegistry(this); }; adapter.Map.prototype.setCenter = function (latlng) { var latlngImpl = latlng ? new google.maps.LatLng(latlng._lat, latlng._lng) : undefined; this.impl.setCenter(latlngImpl); }; adapter.Map.prototype.setMapTypeId = function (mapTypeId) { this.mapTypeId = mapTypeId; this.impl.setMapTypeId(mapTypeId); }; adapter.Map.prototype.getCenter = function () { var latlngImpl = this.impl.getCenter(); return latlngImpl ? new bmcMaps.LatLng(latlngImpl.lat(), latlngImpl.lng()) : undefined; }; adapter.Map.prototype.fitBounds = function (bounds) { var swImpl = bounds && bounds.sw ? new google.maps.LatLng(bounds.sw._lat, bounds.sw._lng) : undefined; var neImpl = bounds && bounds.ne ? new google.maps.LatLng(bounds.ne._lat, bounds.ne._lng) : undefined; var boundsImpl = bounds ? new google.maps.LatLngBounds(swImpl, neImpl) : undefined; this.impl.fitBounds(boundsImpl); }; adapter.Map.prototype.setZoom = function (zoom) { this.impl.setZoom(zoom); }; adapter.Map.prototype.getZoom = function () { return this.impl.getZoom(); }; adapter.Map.prototype.getBounds = function () { var boundsImpl = this.impl.getBounds(); var swImpl = boundsImpl ? boundsImpl.getSouthWest() : undefined; var neImpl = boundsImpl ? boundsImpl.getNorthEast() : undefined; var sw = swImpl ? new bmcMaps.LatLng(swImpl.lat(), swImpl.lng()) : undefined; var ne = neImpl ? new bmcMaps.LatLng(neImpl.lat(), neImpl.lng()) : undefined; return boundsImpl ? new bmcMaps.LatLngBounds(sw, ne) : undefined; }; adapter.Map.prototype.getProjection = function () { return new adapter.Projection(this); }; adapter.Map.prototype.panToBounds = function (bounds) { var swImpl = bounds && bounds.sw ? new google.maps.LatLng(bounds.sw._lat, bounds.sw._lng) : undefined; var neImpl = bounds && bounds.ne ? new google.maps.LatLng(bounds.ne._lat, bounds.ne._lng) : undefined; var boundsImpl = bounds ? new google.maps.LatLngBounds(swImpl, neImpl) : undefined; this.impl.panToBounds(boundsImpl); }; adapter.Map.prototype.panTo = function (latlng) { var latlngImpl = latlng ? new google.maps.LatLng(latlng._lat, latlng._lng) : undefined; this.impl.panTo(latlngImpl); }; adapter.Projection = function (map) { this.impl = map.impl.getProjection(); }; adapter.Projection.prototype.fromPointToLatLng = function (point) { var pointImpl = point ? new google.maps.Point(point.x, point.y) : undefined; var latlngImpl = this.impl.fromPointToLatLng(pointImpl); return new bmcMaps.LatLng(latlngImpl.lat(), latlngImpl.lng()); }; adapter.infoWindow = function (options) { return new adapter.InfoWindow(options); }; adapter.InfoWindow = function (options) { if (options && options.center) { options.center = new google.maps.LatLng(options.center._lat, options.center._lng); } this.impl = new google.maps.InfoWindow(options); }; adapter.InfoWindow.prototype.open = function () { this.impl.open(arguments); }; adapter.richMarker = function (options) { return new adapter.RichMarker(options); }; adapter.RichMarker = function (options) { if (options) { this.position = options.position; this.data = options.data; this.content = options.content; if (options.map) { options.map = options.map.impl; } if (options.position) { options.position = new google.maps.LatLng(options.position._lat, options.position._lng); } } this.impl = new RichMarker(options); }; adapter.RichMarker.prototype.setPosition = function (latlng) { var latlngImpl = latlng ? new google.maps.LatLng(latlng._lat, latlng._lng) : undefined; this.impl.setPosition(latlngImpl); }; adapter.RichMarker.prototype.setVisible = function (visible) { this.impl.setVisible(visible); }; adapter.RichMarker.prototype.destroy = function () { this.impl.onRemove(); }; adapter.infoBubble = function (options) { return new adapter.InfoBubble(options); }; adapter.InfoBubble = function (options) { if (options && options.position) { options.position = new google.maps.LatLng(options.position._lat || options.position.lat(), options.position._lng || options.position.lng()); } this.impl = new InfoBubble(options); }; adapter.InfoBubble.prototype.open = function (map, marker) { this.impl.open(map ? map.impl : undefined, marker ? marker.impl : undefined); }; adapter.InfoBubble.prototype.close = function () { this.impl.close(); }; adapter.InfoBubble.prototype.getContent = function () { return this.impl.getContent(); }; adapter.InfoBubble.prototype.setContent = function (content) { this.impl.setContent(content); }; adapter.markerClusterer = function (map, markers, options) { return new adapter.MarkerClusterer(map, markers, options); }; adapter.MarkerClusterer = function (map, markers, options) { var markersImpl = []; for (var i = 0; i < markers.length; i++) { markersImpl.push(markers[i].impl); } this.impl = new MarkerClusterer(map.impl, markersImpl, options); }; adapter.MarkerClusterer.prototype.clearMarkers = function () { this.impl.clearMarkers(); }; bmcMaps.adapters.register('google', adapter); })();