问题是在中心设置地图以便显示多个标记

时间:2016-05-18 06:59:51

标签: google-maps ionic-framework fitbounds

我正在使用离子框架。我创建了地图和多个标记,价值来自服务器端。所有数据都正常但我不知道为什么会出现此错误 -

Type a word to format. Typing quit will exit the program: monkey
monkey>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<yeknom
Type a word to format. Typing quit will exit the program: 

代码:     angular.module('starter',['ionic','ngCordova'])

ionic.bundle.js:25642 TypeError: Cannot read property 'fitBounds' of null
    at autoCenter (app.js:147)
    at app.js:135
    at processQueue (ionic.bundle.js:27879)
    at ionic.bundle.js:27895
    at Scope.$eval (ionic.bundle.js:29158)
    at Scope.$digest (ionic.bundle.js:28969)
    at Scope.$apply (ionic.bundle.js:29263)
    at done (ionic.bundle.js:23676)
    at completeRequest (ionic.bundle.js:23848)
    at XMLHttpRequest.requestLoaded (ionic.bundle.js:23789)

1 个答案:

答案 0 :(得分:2)

只有map成功时才会创建$cordovaGeolocation.getCurrentPosition,但如果loadMarkers成功,则会调用null(在这种情况下,地图为getCurrentPosition)< / p>

解决方案:在marker - 回调之外创建地图(使用中心的默认值)。

在成功回调中,创建center并设置地图的function initMap() { var options = { timeout: 10000, enableHighAccuracy: true }, mapOptions = { center: new google.maps.LatLng(-37.92,151.25), zoom: zoomVal, mapTypeId: google.maps.MapTypeId.ROADMAP }, map = new google.maps.Map(document.getElementById("map"), mapOptions); google.maps.event.addListenerOnce(map, 'idle', function () { $cordovaGeolocation.getCurrentPosition(options).then(function (position) { var center = new google.maps.LatLng( position.coords.latitude, position.coords.longitude); map.setCenter(center); new google.maps.Marker({ map : map, animation : google.maps.Animation.DROP, position : center }); loadMarkers(map); }, function (error) { loadMarkers(map); }); }); }

:after