Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update map marker in angular application

Tags:

angularjs

So i have created this small map application HERE, and as you can see it shows your current localtion, now the problem is the marker will not show on the current location, If you CTRL+U you will see that the code for the marker is:

<openlayers ol-center="center"  height="400px">
            <ol-marker lat="center.lat" lon="center.lon" message="Your current location." ng-model="center" >
            </ol-marker>
</openlayers>

The lat="center.lat" and lon="center.lon" in the beginning is 0 , now if i hardcode the values of lat and lon to my current location , ofcourse the marker will show in my current location, but how do i update the values dynamically ?

I am using angular.js , openLayers3 and openlayers-angular-directive , So to repeat my question, how do i update the marker dynamically ?

like image 952
Tenali_raman Avatar asked Oct 20 '15 22:10

Tenali_raman


4 Answers

All you need is to add "autodiscover : true" to your center object in your controller.

As it is explained in the documentation of the directive.

Also, you need to put the value bound in the marker:

<openlayers ol-center="center"  height="400px">
        <ol-marker lat="{{center.lat}}" lon="{{center.lon}}" message="Your current location." ng-model="center" >
        </ol-marker>

like image 74
pedromarce Avatar answered Sep 20 '22 01:09

pedromarce


Use $timeout instead of setTimeout. Angular doesn't know you are modifying the scope inside setTimeout so it doesn't update the view.

$timeout uses setTimeout() internally but also calls $apply() to have angular run a digest.

rememeber to also inject it in controller

like image 45
charlietfl Avatar answered Sep 19 '22 01:09

charlietfl


you can try

<ol-marker ol-marker-properties="center" ></ol-marker>

angular.extend($scope, {
            center: {
                lat: 0,
                lon: 0,
                autodiscover: true
                label: {
                    message: 'Your current location.',
                    show: true,
                    showOnMouseOver: true
                }
            }
        });
like image 39
raj Avatar answered Sep 23 '22 01:09

raj


good example with code to work with map in angular JS

http://ngmap.github.io/drawings.html

like image 36
Mitul Avatar answered Sep 21 '22 01:09

Mitul