Skip to content

Instantly share code, notes, and snippets.

@Set2005
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save Set2005/7051f1fab31b057d0de4 to your computer and use it in GitHub Desktop.

Select an option

Save Set2005/7051f1fab31b057d0de4 to your computer and use it in GitHub Desktop.
// This is call after stores loaded, if we init the map before load stores, then
// we don't if the map is available when we want to add markers.
$scope.setupMap = function(position, zoom){
console.log("***************************************");
$scope.mapLoaderTimer = $timeout(function(){
mapService.initialize("map_canvas", position, zoom, function(map){
/*
* set store markers if map is ready
*/
mapService.clear();
angular.forEach($scope.stores, function(store){
if (store.coords){
var position = new plugin.google.maps.LatLng(store.coords.latitude, store.coords.longitude);
mapService.addMarker(
position,
store.name,
null,
function(marker){
marker.addEventListener(plugin.google.maps.event.INFO_CLICK, function() {
$state.go("main."+ $scope.$root.tabName +"-show-store", {id: store.id});
});
});
}
});
});
$scope.onScoll();
}, 300);
}
if (StoreDataService.isPristine() == false){
$scope.loadingDone = true;
if (($scope.stores instanceof Array) && ($scope.stores.length > 0) && ($scope.stores[0].placeholder != true)){
$scope.stores.unshift({placeholder: true});
}
$scope.setupMap();
return
}
if ($scope.$root.positionAccrued != null){
console.log("position accrued, so we can load stores.");
console.log("****request location")
// console.log(JSON.stringify({
// latitude : position.latitude,
// longitude : position.longitude
// }))
StoreDataService.index({
latitude : position.latitude,
longitude : position.longitude
}).then(
function(response){
// console.log("stores data received." + JSON.stringify(response));
$scope.loadingDone = true;
// $scope.storeLoaderCanceler = $timeout(function(){
if (response && response.length > 0) {
// $scope.stores = StoreDataService.data();
$scope.stores = response;
/*
* Add empty item as placeholder to take place of the map
*/
if (($scope.stores instanceof Array) && ($scope.stores.length > 0) && ($scope.stores[0].placeholder != true)){
$scope.stores.unshift({placeholder: true});
}
}
// });
$scope.setupMap();
});
angular.module('CordovaGoogleMap', []).factory("mapService", function($rootScope, $timeout){
var map = undefined;
var center = undefined;
var defaultZoom = 15;
function initialize (div, coords, zoom, _callback){
if (!coords){
coords = $rootScope.currentLocation;
}
try{
center = new plugin.google.maps.LatLng(coords.latitude, coords.longitude);
}catch(e){
console.log("Plugin is not available!");
return
}
if (!center){
throw new Exception("Map cannot init without a center!");
}
var mapDiv = document.getElementById(div);
// Initialize the map plugin
map = plugin.google.maps.Map.getMap(mapDiv);
console.log("map initialized");
map.on(plugin.google.maps.event.MAP_READY, function(intMap){
console.log("....Map started....")
intMap.setOptions({
'controls': {
'compass': true,
'indoorPicker': true,
'myLocationButton': true,
'zoom': false
},
'gestures': {
'scroll': true,
'tilt': true,
'rotate': true
},
'camera': {
'latLng': center,
'zoom': (zoom||15)
}
});
intMap.moveCamera({
'target': center,
'zoom': (zoom||15)
});
intMap.setVisible(true);
intMap.setClickable(true);
// map.clear();
if (_callback){
console.log("***Got map callback")
_callback(intMap);
}
});
}
function moveCamera(lat, lng, zoom) {
var options = {
'target': new plugin.google.maps.LatLng(lat, lng),
'zoom': defaultZoom,
'duration': 1100
}
getCameraPosition(function(camera){
console.log("getting camera");
currentZoom = camera.zoom;
if (zoom){
options['zoom'] = zoom;
} else {
options['zoom'] = currentZoom;
}
if (map){
map.animateCamera(options, function() {
console.log("The animation is done");
});
}
});
};
function getCameraPosition(_callback){
map.getCameraPosition(function(camera) {
if(_callback)
_callback(camera);
});
};
function onMapClickEvent(_callback){
if (map){
$timeout(function(){
map.on(plugin.google.maps.event.MAP_CLICK, function(latLng) {
_callback(latLng);
});
});
}
}
function setclickable(){
map.setClickable(true);
}
function setUnclickable(){
map.setClickable(false);
}
function remove(){
if (map){
map.remove();//remove
// map.setVisible(false);
// map.removeEventListener();
map.setClickable(false);
// map.clear();
}
}
function hide(){
if (map){
map.setVisible(false);
}
}
function show(){
if (map){
map.setVisible(true);
}
}
function addMarker(latLng, title, icon, _callback){
if (!map){
return
}
markerOptions = {
'position': latLng
}
if (title){
markerOptions['title'] = title;
}
if (icon){
markerOptions['icon'] = icon
}
// 'icon': 'www/img/icon-yellow.png'
map.addMarker(markerOptions, function(marker){
if (_callback){
_callback(marker);
}
if (icon){
marker.setIcon({
'url': icon
});
}
});
}
function showDialog(){
if (map){
map.showDialog();
}
};
return{
initialize: function(div, coords, zoom, _callback){
return initialize(div, coords, zoom, _callback);
},
showFullscreen: function(){
showDialog();
},
getCameraPosition: function(_callback){
getCameraPosition(_callback);
},
remove: function(){
remove();
},
hide: function(){
hide();
},
show: function(){
show();
},
moveCamera: function(lat, lng, zoom){
moveCamera(lat, lng, zoom)
},
setUnclickable: function(){
if (map)
map.setClickable(false);
},
setClickable: function(){
if (map)
map.setClickable(true);
},
addMarker: function(latLng, title, icon, _callback){
addMarker(latLng, title, icon, _callback);
},
onMapClickEvent: function(_callback){
onMapClickEvent(_callback);
},
clear: function(){
if(map){
map.clear();
}
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment