8

I want to use ngMap to add Google Maps to my app.

The demos are "static" in the sense that they have only hard coded HTML. I want my code to be "dynamic" in the sense that it will periodically ask a server to look in its database and return me a bunch of coordinates to plot, which will change with time. I hope that that is clear; if not, please ask for more detail.

I modified the ngmap markers demo to generate some random lat/long coordinates every two seconds (rather than going to my server, as my final app will). Please see the Plunk.

There are no errors in the console, and it seems that ngMap is trying to add my markers, as I see a lot of this sort of thing in the console ...

adding marker with options,  
Object {ngRepeat: "myMarker in markers", position: O}
clickable: true
ngRepeat: "myMarker in markers"
position: O
A: 103.96749299999999
k: 1.387454
__proto__: O
visible: true
__proto__: Object

where K and A are the Lat/Long as I expect them to be.

BUT ... I don't see any markers on the map. What am I doing wrong?


[Update] An excellent answer, for which I gladly awarded a bounty afterwards. For anyone else reading this and wishing to use ngMap as @allenhwkim said in another stackoverflow question and, I think, on his blog, ngMap just creates the map for you & after that you manipulate it with the standard Google Maps API.

For instance, just before looping to add the markers, I declared
var bounds = new google.maps.LatLngBounds(); and in the loop, after adding the marker to the map, I bounds.extend(latlng); and, finally, after the loop, I

var centre = bounds.getCenter();  
$scope.map.setCenter(centre);

I forked the answer and created a new Plunk to show this. Not the world's most useful functionality, but the point is just to show how to use $scope.map with the Google Maps API. Thanks again, Allen, for ngMap.

Yiannis Tsimalis
  • 739
  • 2
  • 11
  • 23
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

2 Answers2

26

Answer is here

http://plnkr.co/edit/Widr0o?p=preview

Please remember that ngMap is not replacing Google Maps V3 API.

Let me know if you have further questions.

The following is code block of the controller.

// $scope.map .. this exists after the map is initialized
var markers = [];
for (var i=0; i<8 ; i++) {
  markers[i] = new google.maps.Marker({
    title: "Hi marker " + i
  })
}
$scope.GenerateMapMarkers = function() {
  $scope.date = Date(); // Just to show that we are updating

  var numMarkers = Math.floor(Math.random() * 4) + 4;  // betwween 4 & 8 of them
  for (i = 0; i < numMarkers; i++) {
    var lat =   1.280095 + (Math.random()/100);
    var lng = 103.850949 + (Math.random()/100);
    // You need to set markers according to google api instruction
    // you don't need to learn ngMap, but you need to learn google map api v3
    // https://developers.google.com/maps/documentation/javascript/marker
    var latlng = new google.maps.LatLng(lat, lng);
    markers[i].setPosition(latlng);
    markers[i].setMap($scope.map)
  }      

  $timeout(function() {
    $scope.GenerateMapMarkers(); 
  }, 2000);  // update every 2 seconds
};  

$scope.GenerateMapMarkers();    
BLSully
  • 5,929
  • 1
  • 27
  • 43
allenhwkim
  • 27,270
  • 18
  • 89
  • 122
  • +1, the answer, and a 200 point bonus tomorrow!! Thanks you so much, Allen. Your ngMap seems to be perfect for me. I just want to get a fixed size map drawn and then (use the Goggle Maps API to) add and move Markers (and call SetBounds so that the map encompasses them), change the Marker tooltips, draw PolyLines, etc. This is an excellent toolkit. Btw, perhaps you ought to consider adding this to your examples. with a few words on your blog page for people like me who want to update "dynamic" maps. – Mawg says reinstate Monica Mar 14 '14 at 01:50
  • 1
    @Mawg, thanks for your extra points(You shouldn't have). As you said, I added dynamic markers as an example file, and hopefully it's useful for many. – allenhwkim Mar 17 '14 at 23:18
  • Ah, yes, https://rawgithub.com/allenhwkim/angularjs-google-maps/master/examples/dynamic_markers.html I also forked your Plunk to show how to use the Google Maps API after ngMap creates the map. I had tried a few solutions for Angulr Google Maps but couldn't get any to work. Then I found ngMap and it is everything that I could wish for. Thanks! – Mawg says reinstate Monica Mar 18 '14 at 07:25
  • @allenhwkim your plunker is not working it can't find the ngMap module – Gardezi Aug 19 '15 at 07:24
  • @allenhwkim there is one thing more. Can you tell me how can I animate a marker when I click on a div. for example I have div with a title if I click on it the marker starts bouncing or some other kind of animation starts working – Gardezi Aug 19 '15 at 12:03
  • @allenhwkim the plunker is still not working it can't include the ngmap modeule – Gardezi Aug 19 '15 at 13:27
  • @allenhwkim there is just one thing else that I would like to know and it that where is the $scope.map object – Gardezi Aug 19 '15 at 13:58
  • @allenhwkim Thanks for the solution. But there is a problem with the plunkr. The link used for ngmap.min.js is not working. I've edited it. This is working fine. http://plnkr.co/edit/Widr0o?p=preview – Adeel Raza May 13 '16 at 11:41
5

Why not do something like

<map zoom="2" center="[40.74, -74.18]">
  <marker position="{{destination.position}}" ng-repeat="destination in destinations"></marker>
</map>

If you asking for ng-repeat that would work. And you would populate the destinations with a simple http call to your backend:

$http.get(url + '/destinations', config).success(function (data) {
  if (data != null && data.total > 0) {
      $scope.destinations = data.destinations;
  } else {
      $scope.destinations = []
  }
});
allenhwkim
  • 27,270
  • 18
  • 89
  • 122
Vladimir
  • 2,481
  • 4
  • 31
  • 41
  • This didn't work for me.. I was trying the same thing with Polygons instead of markers, and the polygons don't show up. – yndolok Oct 23 '14 at 23:08
  • Are you getting any errors? Does one show up? Strip down the code and see when it stops working, otherwise I can't really help much :) – Vladimir Oct 25 '14 at 02:44
  • No worries, @allenhwkim's answer worked for me. This feels cleaner though, so I gave it a shot. – yndolok Oct 25 '14 at 03:02
  • 1
    This solution will also work. The difference is the performance. When we use ng-repeat, it will also update DOM, and sometimes updating DOM is not necessary and updating DOM is quite expensive. – allenhwkim Jan 02 '15 at 14:42