![](/img/trans.png)
[英]how to make custom directive in angular for calculate the distance using google maps
[英]How can I force Google Maps to load in my Angular app using an Angular Directive
我遇到一個問題,即我的Google地圖在80%的時間內都無法顯示。 在我的Angular視圖中填充剩余數據時,似乎還沒有完全渲染地圖。
如何強制加載地圖?
我已經進行了一些研究,並在一個相關的問題上找到了這個答案,但是我不確定如何以及是否可以實現以下內容:
使用GMaps v3困擾了我一段時間。 我找到了這樣的方法:
google.maps.event.addListenerOnce(map, 'idle', function(){ // do something only the first time the map is loaded });
當地圖進入空閑狀態時,即觸發“空閑”事件-一切已加載(或加載失敗)。 我發現它比tilesloaded / bounds_changed更可靠,並使用addListenerOnce方法,在第一次觸發“ idle”后執行關閉中的代碼,然后分離事件。
這是我當前的設置:
1.我的Google Maps API鏈接和密鑰位於index.html文件中:
<script src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXX"></script>
2.我將以下內容用作我的Angular指令:
'use strict';
angular.module('portalDashboardApp')
.directive('ogGoogleMap', function ($http, $q) {
return {
restrict: 'E',
scope: {
twitter: '=',
instagram: '='
},
template: '<div id="gmaps"></div>',
replace: true,
link: function (scope, element, attrs) {
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(-0.013026, 21.333860),
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content, icon) {
if (icon === 'IN') {
icon = 'images/instagramMarker.png';
}
else {
icon = 'images/twitterMarker.png';
}
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: icon
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function () {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
function deleteCurrentMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers = [];
}
scope.$watch('instagram', function () {
deleteCurrentMarkers();
populateMarkers(scope.twitter, 'TW');
populateMarkers(scope.instagram, 'IN');
});
// show the map and place some markers
initMap();
function populateMarkers(locationArray, type) {
angular.forEach(locationArray, function (location) {
setMarker(map, new google.maps.LatLng(location[0], location[1]), '', '', type);
});
}
}
};
});
3.我使用以下簡單方法在Angular Controller中分配地圖數據:
首先,我檢索數據:
function pullSocialData() {
SocialMediaUserService.getKeywordProfileID().then(function (keywordProfileID) {
GetFusionDataService.getItems(getRequestURL(keywordProfileID))
.success(function (data) {
formatDataAccordingToLocation(data);
})
.error(function (error, status) {
handleDataRetrievalError(error, status);
});
});
}
我分配我的數據:
function formatDataAccordingToLocation(data) {
$scope.twitterLocations = data.lat_longs_twitter;
$scope.instagramLocations = data.lat_longs_instagram;
}
這是我來自API的數據:
lat_longs_twitter: [
[
-25.77109,
28.09264
],
[
-26.1078272,
28.2229014
]
]
4.我的HTML地圖div:
<div ng-show="!demographics.showDemographicsGraph">
<og-google-map twitter="twitterLocations" instagram="instagramLocations"></og-google-map>
</div>
當我的地圖正確加載時,它看起來像這樣:
如果無法正確加載,則如下所示:
先感謝您!
在指令鏈接函數中,嘗試將initMap
移到地圖idle
回調中:
google.maps.event.addListenerOnce(map, 'idle', function(){
// show the map and place some markers
initMap();
});
為了加載地圖,我添加了一些驗證以在初始化地圖之前檢查數據是否已返回。 我還添加了timeOut
作為很好的度量,以便給地圖更多的渲染時間。
我在“角度指令”中進行了以下更改:
scope.$watch('instagram', function () {
if (scope.twitter != undefined || scope.instagram != undefined) {
initMap();
setTimeout(function () {
deleteCurrentMarkers();
populateMarkers(scope.twitter, 'TW');
populateMarkers(scope.instagram, 'IN');
}, 3000);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.