CptnKrunch CptnKrunch - 6 months ago 214
AngularJS Question

How can I change state using UI Router inside a Google Maps marker?

I'm populating a Google Map instance with markers that have info windows containing a button that's supposed to link to a page in my app with more information about said marker. I have the same button working on a separate view and I copied it directly but I think it's an issue of scope.

Update with code:

var content = '<a ng-click="changeView(\'list\')" ui-sref="productDetail({id:\'' + $scope.products.items[x]._id + '\', slug:\'' + $scope.products.items[x].slug + '\'})" class="btn btn-default">View details</a>';

google.maps.event.addListener(marker, 'click', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,$rootScope.infowindow));

Answer

Since info window is dynamically generated it needs to be compiled using $compile service:

var content = '<a ng-click="changeView(\'list\')" ui-sref="productDetail({id:\'' + $scope.products.items[x]._id + '\', slug:\'' + $scope.products.items[x].slug + '\'})" class="btn btn-default">View details</a>';
var compiledContent = $compile(content)($scope);

google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow) {
      return function() {
         infowindow.setContent(content);
         infowindow.open($scope.map, marker);
      };
})(marker, compiledContent[0], $rootScope.infowindow));

Working example

angular.module('map-example', [])
    .controller('MapController', function($scope, $rootScope, $compile) {


        function initialize() {

            $scope.map = new google.maps.Map(document.getElementById('map'), {
                zoom: 4,
                center: { lat: -34.363, lng: 131.044 }
            });



            $scope.products = {
                items: [
                    { _id: 1, slug: '', lat: -33.873033, lng: 151.231397 },
                    { _id: 2, slug: '', lat: -37.812228, lng: 144.968355 }
                ]
            };


            $rootScope.infowindow = new google.maps.InfoWindow({
                content: ''
            });


            for (var x = 0; x < $scope.products.items.length; x++) {


                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng($scope.products.items[x].lat, $scope.products.items[x].lng),
                    map: $scope.map,
                    title: 'Uluru (Ayers Rock)'
                });

                var content = '<a ng-click="changeView(\'list\')" ui-sref="productDetail({id:\'' + $scope.products.items[x]._id + '\', slug:\'' + $scope.products.items[x].slug + '\'})" class="btn btn-default">View details</a>';
                var compiledContent = $compile(content)($scope)

                google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow) {
                    return function() {
                        infowindow.setContent(content);
                        infowindow.open($scope.map, marker);
                    };
                })(marker, compiledContent[0], $rootScope.infowindow));

            }

        }

        $scope.changeView = function(name) {
            alert(name);
        }

        google.maps.event.addDomListener(window, 'load', initialize);

    });
html, body {
        height: 400px;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 400px;
      }
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.1/lodash.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div ng-app="map-example" ng-controller="MapController">
    <div id="map"></div>
</div>

Update

In case of multiple html elements you could consider the following solution:

  • wrap elements in container element
  • compile the container element

Example

var content = '<h2>Info</h2><a ng-click="changeView(\'list\')" ui-sref="productDetail({id:\'' + $scope.products.items[x]._id + '\', slug:\'' + $scope.products.items[x].slug + '\'})" class="btn btn-default">View details</a>';

google.maps.event.addListener(marker, 'click', (function (marker, content, infowindow) {
      return function () {
        var contentDiv = angular.element('<div/>');
        contentDiv.append(content);
        var compiledContent = $compile(contentDiv)($scope);

        infowindow.setContent(compiledContent[0]);
        infowindow.open($scope.map, marker);
      };
 })(marker, content, $rootScope.infowindow));

Plunker

Comments