angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

Update marker via model change

Open phaeton2040 opened this issue 12 years ago • 23 comments

I'm trying to change marker's title which is already rendered. My model looks like this:

markers: [
            id: counter,
            coordinates: {
                latitude: 57.156727,
                longitude: 65.613536
            },
            options: {
                draggable: true,
                title: 'New point'
            },
            events: {
                dragend: function(e) {
                    $scope.current_coords = {
                        latitude: e.position.k,
                        longitude: e.position.A
                    }
                }
            }
        ],

Markers are rendered on the map as they should. But if i try to change this object for example this way:

var marker = _.find($scope.map.markers, function(marker){
            return marker.id == current_id;
        });
        var index = _.indexOf($scope.map.markers, marker);
        $scope.map.markers[index].options.title = $scope.current_place;
        $scope.map.markers[index].options.draggable = false;

markers on the map are not changed. How should I do this?

phaeton2040 avatar May 16 '14 06:05 phaeton2040

Can you provide a simple example in plunker?

See this: http://plnkr.co/edit/gwis5DXQzalgB5saeaiu?p=preview

rdelafuente avatar May 17 '14 00:05 rdelafuente

Not sure how to save my plunk, please, try this link http://plnkr.co/edit/fz6Kpi?p=preview

phaeton2040 avatar May 17 '14 06:05 phaeton2040

Some typos in your plunker, but I get it working: http://plnkr.co/edit/VVsgH2?p=preview

Now I see what you mean. Yep, it's definitely an issue for me. Anything updated within marker.options won't work. I tried to add a marker-label to see if the title changed there, but no luck. I also noticed that adding a marker-label to a marker makes it undraggable .

Hopefully someone will see this issue and fix it or suggest a workaround.

rdelafuente avatar May 17 '14 15:05 rdelafuente

yeah this looks to be a bug. Did anyone try markers instead?

nmccready avatar May 19 '14 15:05 nmccready

The options property doesn't work on markers (title, draggable, etc). labelContent="'options.title'" doesn't work either, but it updates correctly if used with a prop outside the options object: labelContent="'labelTitle'"

Updated plunker: http://plnkr.co/edit/VVsgH2?p=preview

rdelafuente avatar May 20 '14 15:05 rdelafuente

Ok I will look into this when I get the chance.

nmccready avatar May 20 '14 15:05 nmccready

Let's try to break this into two issues, one for markers and another for marker-label .

nmccready avatar May 25 '14 16:05 nmccready

So for markers the following code causes the title to trip/change (currently):

https://github.com/nlaplante/angular-google-maps/blob/master/src/coffee/directives/api/models/child/marker-child-model.coffee#L108-L109

and

https://github.com/nlaplante/angular-google-maps/blob/master/src/coffee/directives/api/models/child/marker-child-model.coffee#L122-L129

Since you guys are trying to set the title of the labelContent via options nothing will be changed. This is for a couple of reasons. First your directive instance does not define the options attribute as even being watched. So changing options will do nothing. Second, when options does change it is only looking for the following properties:

opts:
    labelAnchor
    labelClass
    labelContent

Again those were not changing.

Lastly, The function is only updating the label content based off the scope here:

opts.labelAnchor = @getLabelPositionPoint(scope.labelAnchor)
          opts.labelClass = scope.labelClass 
          opts.labelContent = scope.labelContent //actual label content is labelTitle! not opt.title

So if you update your controllers marker property of labelTitle it does change. Now if you want thing to change via options changing then we would need to make that improvement. Along with your code actually watching the options to begin with.

Does this make sense?

nmccready avatar May 25 '14 16:05 nmccready

I think there is a memory leak here as well with the original MarkerWithLabel not being erased as well.

nmccready avatar May 25 '14 16:05 nmccready

Also in Markers it appears that none of the extended features like dragging, visible, and optomized and many more are not being used. Instead of instantiating MarkerWithLabel in MarkerChildModel (used by Markers) it should be creating MarkerLabelChildModel instead. Then all the options would be handled in the same manner. https://github.com/nlaplante/angular-google-maps/blob/master/src/coffee/directives/api/models/child/label-child-model.coffee#L10-L67

NVM ignore this as MarkerWithLabel takes care of all that MarkerChildModel does. In reality MarkerChildModel should possibly disappear.

nmccready avatar May 25 '14 16:05 nmccready

So the best fix here is to allow options to be used directly for Label stuff to use options.labelContent.... etc and use scope.labelContent directly. I actually would be more inclined to get rid of the custom attributes all together as they my the whole issue more complex. That way options would be the only thing.

nmccready avatar May 25 '14 17:05 nmccready

Here is the existing plunkr updated and working with labelContent updates via Markers.

http://plnkr.co/edit/6hnO3p

nmccready avatar May 25 '14 17:05 nmccready

Thanks @nmccready ! Let me see if I understood you well:

  1. In my example the options property within the markers2model wasn't working because I forgot to add options="'options'"to the markers directive.
  2. Currently, for markers directive, labelContentcannot be bound to options.whatever (i.e: options.title, options.labelContent), instead it must be bound to a single property within the model (i.e: labelTitle or title).
  3. It would be a great addition to allow options to be used directly for label stuff.

As for the marker directive, if you dynamically change options.title it won't update the "mouseover" title of the marker(s) in the map. Also, if you try to add a marker-label directive the marker(s) will become undraggable and the same issues with label content will appear.

rdelafuente avatar May 26 '14 03:05 rdelafuente

Thanks, @nmccready ! This is the thing I was looking for but wasn't able to find in official docs.

phaeton2040 avatar May 26 '14 07:05 phaeton2040

@rdelafuente yes I think all you stated is correct . As for the last part;

As for the marker directive, if you dynamically change options.title it won't update the "mouseover" title of the marker(s) in the map. Also, if you try to add a marker-label directive the marker(s) will become undraggable and the same issues with label content will appear.

I would need to look this ver again.

@phaeton2040 ahh, would like to fix the docs.

@rdelafuente would you be so kind to fix the issue and PR it?

nmccready avatar May 31 '14 15:05 nmccready

This really should boil down to 3 new tickets.

  1. Documentation fixes/ updates for marker, and maybe markers
  2. Fix marker/ marker-label and make it consistent with markers
  3. Markers should have options that take title/label into account.

Who wants to do what?

nmccready avatar May 31 '14 15:05 nmccready

@nmccready I'll take a look into

  • Fix marker/ marker-label and make it consistent with markers

But first I need to get familiar with CoffeScript and angular-google-maps source code. I'll open an issue for it as soon as I have some news.

rdelafuente avatar Jun 02 '14 00:06 rdelafuente

:+1:

nmccready avatar Jun 02 '14 00:06 nmccready

Guys, I'm looking into this, because I need to be able to update markers on a map and change opacity to some of them.

How can I do this with angular-google-maps since updating options.opacity doesnt actually do anything? @nmccready @rdelafuente

antitoxic avatar Oct 22 '14 09:10 antitoxic

@antitoxic I don't see why this should not work. Make a new issue with a plnkr.

nmccready avatar Oct 22 '14 13:10 nmccready