Skip to content

Instantly share code, notes, and snippets.

@sp90
Created May 26, 2016 11:22
Show Gist options
  • Save sp90/5f0ad748d70ccd6641b90d22e05793cf to your computer and use it in GitHub Desktop.
Save sp90/5f0ad748d70ccd6641b90d22e05793cf to your computer and use it in GitHub Desktop.
Just a solution to google street view no imagry, needs some rewriting but it work
angular
.module('bolighed')
.directive('locationImage', LocationImage);
function LocationImage($q, $sce, $window, streetviewService) {
var apiKey = window.bolighed.google_api_key;
var staticMapApiEndPoint = 'https://maps.googleapis.com/maps/api/staticmap';
var streetviewApiEndPoint = 'https://maps.googleapis.com/maps/api/streetview';
function determineMapType(desiredMapType, fallbackMapType, latitude, longitude) {
var deferredResult = $q.defer();
if (desiredMapType !== 'streetview') {
deferredResult.resolve(desiredMapType);
} else {
streetviewService.loaded.then(function (service) {
service.hasStreetview(latitude, longitude).then(
function (available) {
deferredResult.resolve(available ? desiredMapType : fallbackMapType);
},
function () { // We essentially don't know -- google api call failed
deferredResult.resolve(fallbackMapType);
}
);
});
}
return deferredResult.promise;
}
function buildStreetviewApiUrl(size, location) {
var params = [
'key=' + apiKey,
'size=' + size,
'location=' + location
];
return streetviewApiEndPoint + '?' + params.join('&');
}
function buildStaticMapApiUrl(mapType, zoom, size, location, hasMarker) {
var params = [
'key=' + apiKey,
'size=' + size,
'zoom=' + zoom,
'center=' + location,
'maptype=' + mapType
];
if (_.isUndefined(hasMarker) || hasMarker === 'true') {
var markerString = 'markers=' + location;
params.push(markerString);
}
return staticMapApiEndPoint + '?' + params.join('&');
}
return {
restrict: 'E',
replace: true,
scope: true,
templateUrl: '/static/templates/components/location-image.html',
link: function(scope, element, attrs) {
outlet(scope, 'desiredMapTypeNotAvailable');
if (!_.isEmpty(attrs.latitude) && !_.isEmpty(attrs.longitude)) {
scope.latitude = parseFloat(attrs.latitude);
scope.longitude = parseFloat(attrs.longitude);
}
var desiredMapType = attrs.type || scope.mapType || 'hybrid';
var zoom = attrs.zoom || scope.zoom || '17';
function updateSize() {
if (element.is(':visible')) {
var newSize = element.width() + 'x' + element.height();
if (newSize !== scope.size) {
scope.size = newSize;
}
}
}
angular.element($window).resize(updateSize.bind(this));
scope.$watchGroup(['size', 'latitude', 'longitude', 'location'], function (where) {
var size = where[0];
var latitude = where[1];
var longitude = where[2];
var location = where[3];
if (latitude && longitude) {
location = '' + latitude + ',' + longitude;
}
determineMapType(desiredMapType, 'roadmap', latitude, longitude).then(function (mapType) {
if (mapType !== desiredMapType) {
scope.desiredMapTypeNotAvailable(desiredMapType, mapType);
}
var apiUrl;
if (mapType === 'streetview') {
apiUrl = buildStreetviewApiUrl(size, location);
} else {
apiUrl = _.isUndefined(attrs.placeholderUrl) ? buildStaticMapApiUrl(mapType, zoom, size, location, attrs.fallbackMarker) : attrs.placeholderUrl;
}
element.find('img').attr('src', $sce.trustAsResourceUrl(apiUrl));
});
});
updateSize();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment