I am using ng-map and trying to show multiple markers on the google map. When the page loads I would like to see all the markers on the map scattered across countries.
The map is showing only a zoomed in portion of one marker. May be this is because I am assigning the map center to the last item in the array. When I scroll/zoom in and out of the map, I can then see all the markers.
I am not sure what the map center should be when there are multiple markers and how do I get a map view that will show all the markers when the map loads
Here is the code:
controller:
$scope.cityMetaData = [];
$scope.getCityInfo = function(){
for(var i=0; i<$scope.cityIds.length; i++){
var id = $scope.cityIds[i];
GetCity.query({cityid: id}, function(data) {
if (data.status == "success") {
var cityData = data;
$scope.cityMetaData.push(cityData);
$scope.addressMarker(cityData);
}
});
}
}
$scope.addressMarker = function(cityItem) {
var address = cityItem.cityName;
geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address' : address
}, function(results, status) {
if ( status == google.maps.GeocoderStatus.OK ) {
$scope.lat = results[0].geometry.location.lat();
$scope.lng = results[0].geometry.location.lng();
$scope.markerData.push({
"latitude" : results[0].geometry.location.lat(),
"longitude" : results[0].geometry.location.lng(),
"title" : results[0].formatted_address
});
} else {
$log.info('Geocode was not successful for the following reason:' + status);
}
});
}
$scope.getCityInfo();
html:
<map center="[{{lat}},{{lng}}]" zoom="8" style="display:block; width:auto; height:auto;">
<marker ng-repeat="pin in markerData" position="{{pin.latitude}},{{pin.longitude}}" title="{{pin.title}}"></marker>
</map>
sample $scope.markerData array :
[{"latitude":38.232417,"longitude":-122.6366524,"title":"Petaluma, CA, USA"},
{"latitude":34.1477849,"longitude":-118.14451550000001,"title":"Pasadena, CA, USA"},
{"latitude":40.7556818,"longitude":-73.8830701,"title":"Jackson Heights, Queens, NY, USA"},
{"latitude":32.7766642,"longitude":-96.79698789999998,"title":"Dallas, TX, USA"},
{"latitude":37.7974273,"longitude":-121.21605260000001,"title":"Manteca, CA, USA"},
{"latitude":37.48521520000001,"longitude":-122.23635480000002,"title":"Redwood City, CA, USA"}]