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 location is not consistent #387

Closed jonathanng closed 10 years ago

jonathanng commented 10 years ago

I find that the marker moves after every refresh. I hard coded the longitude latitude but I'm still getting inconsistent placements.

:coffee
    class RichMarkerBuilder extends Gmaps.Google.Builders.Marker

    #
    #           create_marker: ->
    #               options = _.extend @marker_options(), @rich_marker_options()
    #               @serviceObject = new RichMarker options
    #
    #           rich_marker_options: ->
    #               boxText = document.createElement("div")
    #               boxText.setAttribute("class", 'yellow')
    #               boxText.innerHTML = @args.infowindow
    #               _.extend(@marker_options(), { content: boxText })
    #
    #           create_infowindow: ->
    #               return null unless _.isString @args.infowindow
    #
    #               boxText = document.createElement("div")
    #               boxText.setAttribute("class", 'yellow') #to customize
    #               boxText.innerHTML = @args.infowindow
    #               @infowindow = new InfoBox(@infobox(boxText))
    #
    #               @bind_infowindow()
    #
    #           infobox: (boxText)->
    #               content: boxText
    #               pixelOffset: new google.maps.Size(-140, 0)
    #               boxStyle:
    #                   width: "280px"
    #

    handler = Gmaps.build 'Google', { builders: { Marker: RichMarkerBuilder} }

    handler.buildMap
        provider: {}
        internal:
            id: "l_map"
    , ->
        json =
            lat:            37.86915,
            lng:            -122.254796,
            picture:
                anchor:     [ 12, 39 ],
                url:        '/assets/sprite/w24_location_red.png'
                width:      24
                height:     39
            shadow:
                anchor:     [ 12, 6 ]
                url:        '/assets/sprite/w24_location_shadow.png'
                width:      24
                height:     12

        markers = handler.addMarkers [json]
        handler.bounds.extendWith markers
        handler.fitMapToBounds()
        handler.getMap().setZoom 16

image

image

yoyos commented 10 years ago

Yep, came to say that too. How could we fix this ? is it a round problem or an ImageMarker problem ?

apneadiving commented 10 years ago

This is documented here: https://github.com/apneadiving/Google-Maps-for-Rails/blob/master/vendor/assets/javascripts/gmaps/objects/handler.coffee#L5

Before you change it, be aware of its use: people sometimes provide a vague address, like only zip code or only city. In this case, when several people do that, you end up with overlapping markers. Thats why I randomize them slightly.

to remove this do:

handler = Gmaps.build 'Google', { builders: { Marker: RichMarkerBuilder}, markers: { maxRandomDistance: null } }
jonathanng commented 10 years ago

Cool. I added changes to the readme.

jobinthepast commented 10 years ago

Great thanks!

I changed to

handler = Gmaps.build 'Google', { markers: { maxRandomDistance: null } }

and it works. With the builders options set the map was blank (all white).