allenhwkim / angularjs-google-maps

The Simplest AngularJS Google Maps V3 Directive
http://ngmap.github.io
MIT License
1.52k stars 516 forks source link

cannot update view when marker is dragged #857

Closed beshad closed 6 years ago

beshad commented 6 years ago

Hi guys,

I am having an issue with scoping it seems. i can not update the coordinates in my view when marker is dragged. i call a function in my controller when marker dragged and can console log the movement however the scope inside this function is not the controller scope so unable to pass on this changes to controller scope and update the view. need to do something exactly like this fiddle which works fine but the same code fails in my project (http://jsfiddle.net/3J25M/1068/).

here is my code:

HTML:

{{$ctrl.latlng}} <ng-map id="map" center="{{$ctrl.region \|\| 'current-location'}}" zoom="14">   
       <marker position="{{$ctrl.marker.latitude}},{{$ctrl.marker.longitude}}" on-dragend="$ctrl.getPosition($event)" draggable="true">   
     </marker> 
</ng-map>

CONTROLLER:   

getPosition(event) {      
    this.latlng = [event.latLng.lat(), event.latLng.lng()];  
}

when i console.log(this) inside this function it logs the marker directive scope and there is no way for me update the controller. i need the lat and lng to autofill a form when user drag the marker. i am using AngularJS fullStack.

any help would be appreciated,

thanks

allenhwkim commented 6 years ago

I don't think this is the correct one.

beshad commented 6 years ago

yes this inside the getPosition() method is not the MainController this hence i can't manipulate the main controller when marker is dragged. i am missing something simple here i know but can't figure out what!

allenhwkim commented 6 years ago

Take look at this. https://plnkr.co/edit/ehYlVInH2h0h1QreoG3C?p=preview

beshad commented 6 years ago

brilliant mate thank you so much. that solved my problem. a quick question, am i able to get elevation/altitude through this library too? cheers