apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.26k stars 382 forks source link

Marker position inconsistent when switching between default marker icon and custom icon #416

Closed cgat closed 10 years ago

cgat commented 10 years ago

Assume I already have a google map loaded with gmaps4rails and that I have the javascript variable 'handler'. Consider adding a marker the following two ways:

//method 1
var marker_options = { lat: 52.1694722222222, link: "http://localhost:3000/stations/3", lng: -114.186472222222, picture: { url: "/assets/meat_base/blue_marker.png"} }
handler.addMarker(marker_options)
//marker will appear on the map

//method 2
marker = new google.maps.Marker( { position: google.maps.LatLng(marker_options.lat, marker_options.lng), icon: marker_options.picture.url})
//new marker will appear on the map. Notice that it is not in the same location

//method 3
delete marker_options.picture
handler.addMarker(marker_options)
//marker (without icon), will appear in the same place as method 2

I think this may be because you still you MarkerImage, which is now depreciated. For example, if you set a breakpoint at setMap in Gmaps.Google.Objects.Common, you can change the serviceObjects's icon property to the url string (instead of the MarkerImage) and it will work properly.

Any idea how to get around this for the time being?

Also note that this doesn't seem to be related to #387 ( I'm setting markers: { maxRandomDistance: null } in my buildMap function).

I can provide a live demo if you pm me.

apneadiving commented 10 years ago

Not really sure if its due to MarkerImage.

I guess you should explicitly pass your anchor for your custom marker.

cgat commented 10 years ago

I actually set the anchor in the tests I was doing above, but after stepping through the code one more time I see that previously used marker images are cached, so the anchor wasn't getting set. I set the anchor where I create my markers and that seemed to do the trick.

Thanks for the quick reply.

apneadiving commented 10 years ago

good news! please close if it solves your issue :)