0

I am having a very hard time coping with AngularJS and its way of setting up maps and markers.

Right now I have the following code:

<map data-ng-model="mymapdetail" zoom="11" 
              center="{{item.coordinates}}" style="heigth:375px">
<div data-ng-model="mymarker"></div>
</map>

This correctly shows a centered map. Then I am trying to add a marker this way.

$scope.mymarker = new google.maps.Marker({
            map: $scope.mymapdetail,
            position: new google.maps.LatLng(item.coordinates),
            title: woa.title
        });

This code does not produce any results. Where is it wrong?

1
  • Is there a reason why your are not using the angular maps directive? If not then I would provide an answer using that directive. Commented Feb 16, 2015 at 22:39

1 Answer 1

1

Here's working solution with marker appearing on click and this one with marker already visible - using your code (well almost - I didn't use map directive), but you should be able to adapt it to your code with very little changes. Probably the main reason why it wasn't working in your case was using new google.maps.LatLng(item.coordinates)

Here's the code:

//Angular App Module and Controller
angular.module('mapsApp', [])
.controller('MapCtrl', function ($scope) {
    var item = {
        coordinates: [40.6423926, -97.3981926]
    };

    var woa = {
        city: 'This is my marker. There are many like it but this one is mine.'
    };


    //set up map
    var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(40.0000, -98.0000),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

    $scope.mymapdetail = new google.maps.Map(document.getElementById('map'), mapOptions);

    //add marker
    $scope.mymarker = new google.maps.Marker({
        map: $scope.mymapdetail,
        animation: google.maps.Animation.DROP,
        position: new google.maps.LatLng(item.coordinates[0], item.coordinates[1]),
        title: woa.city
    });

});

Let me know if it works for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I forgot to mention that 'new google.maps.LatLng(item.coordinates)' wasn't working because item.coordinates was an array and the google.maps.LatLnt was expecting two integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.