Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing map and markers with angular-google-maps

Hi I am using the angular-google-maps for my project. The html and js code is as follows

html:

        <ui-gmap-markers models="apartments" coords="'loc'" icon="'assets/images/map_icon_normal.png'" idkey="'_id'"
                     fit="'false'" click="click">


      <ui-gmap-windows show="'show'" >
        <div ng-non-bindable>{{streetAddress}}</div>

      </ui-gmap-windows>
    </ui-gmap-markers>



  </ui-gmap-google-map>

Script: angular.module('myApp')

.controller('MapCtrl',
                    function ($scope, $routeParams, Map, uiGmapGoogleMapApi) {
 $scope.apartments = [];
 $scope.customIcon = "../../assets/images/map_icon_normal.png"

$scope.map = {
      center: {
        latitude: $routeParams.lat,
        longitude: $routeParams.lon
      },

      zoom: 5,
      bounds: {},
      events: {
        tilesloaded: function (map) {
          //console.log('tiles loaded');
          $scope.$apply(function () {
            $scope.mapInstance = map;
            //console.log(map.getBounds().getNorthEast());
            $scope.searchbox.options.bounds = new google.maps.LatLngBounds($scope.mapInstance.getBounds().getNorthEast(),
              $scope.mapInstance.getBounds().getSouthWest());

          });
        },
        idle: function(map) {

          $scope.map.refresh = false;

        },
        resize: function(map) {
          console.log('resize');
        },
        dragend: function() {

        }
      },
      markersEvents: {
        click: function(marker, eventName, model, args) {
          console.log('markerEvent click');
          $scope.map.window.model = model;
          $scope.map.window.show = true;
        }
      },

      window :  {
        marker: {},
        show: false,
        closeClick: function() {
          this.show = false;
        },
        options: {} // define when map is ready
      },

      control: {},
      refresh: function () {
        $scope.map.control.refresh();
      }
    }

    uiGmapGoogleMapApi.then(function (map) {

      $scope.getData(20, 0, map);

      map.visualRefresh = true;
      $scope.mapInstance = map;


    })

       $scope.getData = function(limit, offset, map) {
      Map.getApartments($routeParams.lat, $routeParams.lon, limit, offset).success(function (data) {
         ///----- I get undefined error here---
              $scope.map.control.refresh();



      });

    }})
}

I am not sure how to refresh the map with the new markers or even trigger an update to the map. I played around with 'control' and 'refresh' params of the ui-gmap-google-map but cannot get it to work.

like image 964
rOrlig Avatar asked Sep 29 '22 12:09

rOrlig


People also ask

How do you refresh a marker on Google Maps?

To reload the markers, when you create then, push them to an array. Then create a function where you iterate through the array, setting the markers map as null. After this, erase the array.

Can you change markers on Google Maps?

Open the map styles dialog Click on the map tab you want to change. If you don't see a gray bar labeled "Configure map" above the map itself, click "Tools > Change map". If "Feature map" is not highlighted in red, click it. Click the "Change feature styles" button.

How do I move a marker smoothly on Google Maps?

The initialize() function creates a Google Map with a marker. The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

Does Google Maps use angular?

The new Angular Component pearl-lullaby (v9. 0.0-rc. 0) introduces the second official @angular/component component, a Google Maps component.


2 Answers

You need to use uiGmapIsReady --- not uiGmapGoogleMapApi --- to wait for control objects to be augmented with their methods (such as refresh).

The uiGmapIsReady service provides a promise which resolves when all uiGmapGoogleMap directives have been fully initialized, while the uiGmapGoogleMapApi service resolves simply when the Google Maps JS API has loaded.

See this answer for an example of using uiGmapIsReady. And of course, don't forget the docs.

Regarding how to get updates to your models property on the scope to cause updates to your map, your code example needs to be fleshed out and cleaned up to help you there. (For example, what is the resolution of getApartments doing to update $scope.apartments? Or maybe asking that question is itself the answer?) Take a look at this fiddle, it might help you in the meantime.

like image 143
William Avatar answered Oct 13 '22 20:10

William


Try dorebuildall attribute.

<ui-gmap-markers 
    models="apartments"
    coords="'loc'"
    icon="'assets/images/map_icon_normal.png'"
    idkey="'_id'"
    fit="'false'"
    click="click"
    dorebuildall="true">
        <ui-gmap-windows></ui-gmap-windows>
</ui-gmap-markers>

More details at: http://angular-ui.github.io/angular-google-maps/

like image 38
Vinay K Avatar answered Oct 13 '22 21:10

Vinay K