Search code examples
javascriptangularjsgoogle-mapsng-map

AngularJS Google Map (ng-map) icon url not working


For some reason my markers are not showing when i use an absolute url instead of a relative one.

In my view i use:

<marker ... icon="{url: '{{markerSrc}}'}"></marker>

When i do this (in my controller):

$scope.markerSrc = "/images/marker.png";

everything works and the icon is shown

but when i do:

$scope.markerSrc = "http://complete/path/to/marker/images/marker.png";

i get an error:

GET http://localhost:9000/%7Burl:%20'http://complete/path/to/marker/images/marker.png'%7D 404 (Not Found)

i tried different methods like

<marker ... icon="{url: "'{{markerSrc}}'" }"></marker>
<marker ... icon="{url: "{{markerSrc}}" }"></marker>
<marker ... icon="{url: {{markerSrc}} }"></marker>

but none seem to work...


Solution

  • Try to change marker directive icon attribute syntax from:

    <marker ... icon="{url: '{{markerSrc}}'}"></marker>
    

    to

    <marker ... icon="{{markerSrc}}"></marker>
    

    Example

    var app = angular.module('appMaps', ['ngMap']);
    app.controller('mapCtrl', function ($scope) {
        
    
        $scope.cities = [
           { id: 1, name : "Brisbane", location: [-33.867396, 151.206854] },
            { id: 2, name: "Sydney", location: [-27.46758, 153.027892] },
            { id: 3, name: "Perth", location: [-31.953159, 115.849915] }
        ];
    
    
        $scope.markerSrc = "http://maps.google.com/mapfiles/marker.png";
    
    });
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
    <div ng-app="appMaps" ng-controller="mapCtrl">
            <map center="[-24.667856, 133.630694]" zoom="4">
                <marker id='ID{{city.id}}' ng-repeat="city in cities" position="{{city.location}}" icon="{{markerSrc}}"   >
                </marker>
                
            </map>
    </div>