this is my Controller…
.controller(‘MapCurrentCtrl’,function($scope,$compile,$state,$rootScope){
var directionsDisplay = new google.maps.DirectionsRenderer;
var directionsService = new google.maps.DirectionsService;
var pos,infowindow;
var markers=[];
var MARKER_PATH = ‘https://developers.google.com/maps/documentation/javascript/images/marker_green’;
// Get geo coordinates
function getMapLocation() {
//Button "Find me"
$scope.myPlaces = function(){
$scope.map.setCenter(pos);
$scope.map.setZoom(15);
}
navigator.geolocation.getCurrentPosition
(onMapSuccess, onMapError, { enableHighAccuracy: false });
}
// Success callback for get geo coordinates
var onMapSuccess = function (position) {
getMap(position.coords.latitude, position.coords.longitude);
}
// Get map by using coordinates
function getMap(latitude, longitude) {
pos = new google.maps.LatLng(latitude,longitude);
var mapOptions = {
center: pos,
zoom: 13,
mapTypeControl: false,
streetViewControl: false,
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.RIGHT_CENTER
}
};
$scope.map = new google.maps.Map(document.getElementById('hospitalmap'),
mapOptions);
//Add searchbox panel into maps
$scope.map.controls[google.maps.ControlPosition.RIGHT_CENTER].push(document.getElementById('btnGetCurrent'));
$scope.map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(document.getElementById('DirDistancePanel'));
marker = new google.maps.Marker({
position : pos,
map : $scope.map,
title : 'You are here !!!...'
});
var request = {
location: pos,
radius: 500,
types: ['hospital']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService($scope.map);
service.nearbySearch(request, callback);
// var itemTitles = $rootScope.hospitallists.map(function(items){
// return items.title;
// });
function callback(results,status) {
if(status == google.maps.places.PlacesServiceStatus.OK){
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var totalDist;
var totalTime;
var marker = new google.maps.Marker({
map: $scope.map,
icon: 'img/icon-findingmedicalproviders.png',
position: place.geometry.location
});
//Sự kiện click vào marker xung quanh
google.maps.event.addListener(marker, 'click', function() {
//Chỉ dẫn đường
directionsService.route({
origin: pos,
destination: place.geometry.location,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
directionsDisplay.setMap($scope.map);
directionsDisplay.setOptions( { suppressMarkers: true } );
var myroute = response.routes[0];
for (i = 0; i < myroute.legs.length; i++) {
totalDist = myroute.legs[i].distance.value;
totalTime = myroute.legs[i].duration.value;
}
//Mở khung info
var request = {
reference: place.reference
};
service.getDetails(request, function(details, status) {
var contentString = '<div><strong>'+
details.name +'</strong></br>'+
details.formatted_address +'</br>'+
details.website +'</br>'+
details.formatted_phone_number+'</br>'+
'Khoảng cách : '+totalDist+'m'+'</br>'+'</br>'+
'<button ng-click="btnBooking()" style="background-color: #154897;border: none;color: white;text-align: center;">Booking</button></div>';
var compiled = $compile(contentString)($scope);
infowindow.setContent(compiled[0]);
infowindow.open($scope.map, marker);
$scope.btnBooking = function(){
$state.go('app.medicalconcierge');
}
});
} else {
window.alert('Tên bệnh viện không được để trống');
}
});
});
}
}
// Success callback for watching your changing position
var onMapWatchSuccess = function (position) {
var updatedLatitude = position.coords.latitude;
var updatedLongitude = position.coords.longitude;
if (updatedLatitude != Latitude && updatedLongitude != Longitude) {
Latitude = updatedLatitude;
Longitude = updatedLongitude;
getMap(updatedLatitude, updatedLongitude);
}
}
// Error callback
function onMapError(error) {
console.log('code: ’ + error.code + ‘\n’ +
'message: ’ + error.message + ‘\n’);
switch (error.code) {
case error.PERMISSION_DENIED:
alert(‘Người dùng không cho phép định vị’);
break;
case error.POSITION_UNAVAILABLE:
alert(‘Không định vị được thông tin vị trí’);
break;
case error.TIMEOUT:
alert(‘Quá thời gian cho phép’);
break;
}
}
// Watch your changing position
function watchMapPosition() {
return navigator.geolocation.watchPosition
(onMapWatchSuccess, onMapError, { enableHighAccuracy: false });
}
getMapLocation();
})