Javascript 无法阻止单击标记时自动平移到标记(谷歌地图)
我使用google maps api v3、gmaps4rails和infobox,但我不知道如何删除一个事件,即在单击地图后立即在标记上自动平移地图 标记从我的控制器发送:Javascript 无法阻止单击标记时自动平移到标记(谷歌地图),javascript,google-maps,google-maps-api-3,gmaps4rails,Javascript,Google Maps,Google Maps Api 3,Gmaps4rails,我使用google maps api v3、gmaps4rails和infobox,但我不知道如何删除一个事件,即在单击地图后立即在标记上自动平移地图 标记从我的控制器发送: Gmaps4rails.build_markers(experiences) do |experience, marker| marker.lat experience.latitude marker.lng experience.longitude marker.infowindow render_to_st
Gmaps4rails.build_markers(experiences) do |experience, marker|
marker.lat experience.latitude
marker.lng experience.longitude
marker.infowindow render_to_string(partial: "/trip_experiences/infowindow.html.erb", locals: {
experience: experience,
trip: trip
})
marker.title experience.name
end
我的地图是在js中构建的,标记是通过调用处理程序上的addMarkers创建的:
handler=Gmaps.build'Google',{builders:{Marker:InfoBoxBuilder};
handler.buildMap{provider:mapOptions,内部:{id:'map'}},函数{
$.geturl,functiondata{
handler.removeMarkersmarkers;
markers=handler.addMarkersdata;
设置旋转木马窗口;
handler.bounds.extendWithmarkers;
Callbackbalse
};
};
到目前为止,我已经尝试在地图选项中使用Autopan:true,将标记设置为不可点击,然后单击添加一个侦听器,并进行了大量研究,但我没有发现类似的结果。。。所以我猜我做错了什么,但找不到什么!!
任何帮助都将不胜感激。。。。许多塔恩克人
编辑:
正如@apReading所建议的,我试图覆盖自定义生成器中的infowindow_绑定方法,以便删除@markers.panTo行,但当单击它时,地图仍然自动居中于标记上。。。。
以下是自定义生成器的代码:
`
`
任何帮助都会很好。。。。非常感谢我也采纳了阿纳丁的建议,效果很好。我将以下内容放在assets/javascripts/gmaps4rails-infoxbox.coffee中。正如您所看到的,我所做的只是从gem中取出整个代码,然后注释掉关于平移的行
class @InfoBoxBuilder extends Gmaps.Google.Builders.Marker # inherit from base builder
@CURRENT_INFOWINDOW: undefined
@CACHE_STORE: {}
# args:
# lat
# lng
# infowindow
# marker_title
# picture
# anchor: [x,y]
# url
# width
# height
# shadow
# anchor: [x,y]
# url
# width
# height
# provider options:
# https://developers.google.com/maps/documentation/javascript/reference?hl=fr#MarkerOptions
# internal_options
# singleInfowindow: true/false
# maxRandomDistance: null / int in meters
constructor: (@args, @provider_options = {}, @internal_options = {})->
@before_init()
@create_marker()
@create_infowindow_on_click()
@after_init()
build: ->
@marker = new(@model_class())(@serviceObject)
create_marker: ->
@serviceObject = new(@primitives().marker)(@marker_options())
create_infowindow: ->
return null unless _.isString @args.infowindow
new(@primitives().infowindow)({content: @args.infowindow })
marker_options: ->
coords = @_randomized_coordinates()
base_options =
title: @args.marker_title
position: new(@primitives().latLng)(coords[0], coords[1])
icon: @_get_picture('picture')
shadow: @_get_picture('shadow')
_.extend @provider_options, base_options
create_infowindow_on_click: ->
@addListener 'click', @infowindow_binding
infowindow_binding: =>
@constructor.CURRENT_INFOWINDOW.close() if @_should_close_infowindow()
# @marker.panTo()
@infowindow ?= @create_infowindow()
return unless @infowindow?
@infowindow.open( @getServiceObject().getMap(), @getServiceObject())
@marker.infowindow ?= @infowindow
@constructor.CURRENT_INFOWINDOW = @infowindow
_get_picture: (picture_name)->
return null if !_.isObject(@args[picture_name]) || !_.isString(@args[picture_name].url)
@_create_or_retrieve_image @_picture_args(picture_name)
_create_or_retrieve_image: (picture_args) ->
if @constructor.CACHE_STORE[picture_args.url] is undefined
@constructor.CACHE_STORE[picture_args.url] = new(@primitives().markerImage)(picture_args.url, picture_args.size, picture_args.origin, picture_args.anchor , picture_args.scaledSize)
@constructor.CACHE_STORE[picture_args.url]
_picture_args: (picture_name)->
{
url: @args[picture_name].url
anchor: @_createImageAnchorPosition @args[picture_name].anchor
size: new(@primitives().size)(@args[picture_name].width, @args[picture_name].height)
scaledSize: null
origin: null
}
_createImageAnchorPosition : (anchorLocation) ->
return null unless _.isArray anchorLocation
new(@primitives().point)(anchorLocation[0], anchorLocation[1])
_should_close_infowindow: ->
@internal_options.singleInfowindow and @constructor.CURRENT_INFOWINDOW?
_randomized_coordinates: ->
return [@args.lat, @args.lng] unless _.isNumber(@internal_options.maxRandomDistance)
#gives a value between -1 and 1
random = -> (Math.random() * 2 - 1)
dx = @internal_options.maxRandomDistance * random()
dy = @internal_options.maxRandomDistance * random()
Lat = parseFloat(@args.lat) + (180/Math.PI)*(dy/6378137)
Lng = parseFloat(@args.lng) + ( 90/Math.PI)*(dx/6378137)/Math.cos(@args.lat)
return [Lat, Lng]
我不知道在哪里设置itI我不熟悉ruby on rails,但是您必须在这里设置InfoWindowIt在构建器中处理的选项:因为您有一个自定义的构建器,所以您可以重写函数并删除此行非常感谢您的反馈!我将尝试重写该方法,但我不知道js/coffee中的任何内容。同时,我将数据从控制器传递到json中构建infobox,并直接在js中的视图中创建infobox。。。不是很优化@首先,非常感谢你的宝石,它非常简单。干得好!嘿,Dupleix公寓,你把它弄好了吗?我试图做一些类似的事情,通过复制引用marker.coffee的整个文件并将其用作infoboxbuilder类,我成功地实现了它,然后我注释掉了有问题的一行。Hi@kindofgreat,不幸的是,我并没有按照指示去做,这是为了覆盖函数。。。自动扫描仍在工作。。。有效的方法是直接在视图中创建信息框。。。但如果你最终破解了这个问题,我愿意接受更新:
class @InfoBoxBuilder extends Gmaps.Google.Builders.Marker # inherit from base builder
@CURRENT_INFOWINDOW: undefined
@CACHE_STORE: {}
# args:
# lat
# lng
# infowindow
# marker_title
# picture
# anchor: [x,y]
# url
# width
# height
# shadow
# anchor: [x,y]
# url
# width
# height
# provider options:
# https://developers.google.com/maps/documentation/javascript/reference?hl=fr#MarkerOptions
# internal_options
# singleInfowindow: true/false
# maxRandomDistance: null / int in meters
constructor: (@args, @provider_options = {}, @internal_options = {})->
@before_init()
@create_marker()
@create_infowindow_on_click()
@after_init()
build: ->
@marker = new(@model_class())(@serviceObject)
create_marker: ->
@serviceObject = new(@primitives().marker)(@marker_options())
create_infowindow: ->
return null unless _.isString @args.infowindow
new(@primitives().infowindow)({content: @args.infowindow })
marker_options: ->
coords = @_randomized_coordinates()
base_options =
title: @args.marker_title
position: new(@primitives().latLng)(coords[0], coords[1])
icon: @_get_picture('picture')
shadow: @_get_picture('shadow')
_.extend @provider_options, base_options
create_infowindow_on_click: ->
@addListener 'click', @infowindow_binding
infowindow_binding: =>
@constructor.CURRENT_INFOWINDOW.close() if @_should_close_infowindow()
# @marker.panTo()
@infowindow ?= @create_infowindow()
return unless @infowindow?
@infowindow.open( @getServiceObject().getMap(), @getServiceObject())
@marker.infowindow ?= @infowindow
@constructor.CURRENT_INFOWINDOW = @infowindow
_get_picture: (picture_name)->
return null if !_.isObject(@args[picture_name]) || !_.isString(@args[picture_name].url)
@_create_or_retrieve_image @_picture_args(picture_name)
_create_or_retrieve_image: (picture_args) ->
if @constructor.CACHE_STORE[picture_args.url] is undefined
@constructor.CACHE_STORE[picture_args.url] = new(@primitives().markerImage)(picture_args.url, picture_args.size, picture_args.origin, picture_args.anchor , picture_args.scaledSize)
@constructor.CACHE_STORE[picture_args.url]
_picture_args: (picture_name)->
{
url: @args[picture_name].url
anchor: @_createImageAnchorPosition @args[picture_name].anchor
size: new(@primitives().size)(@args[picture_name].width, @args[picture_name].height)
scaledSize: null
origin: null
}
_createImageAnchorPosition : (anchorLocation) ->
return null unless _.isArray anchorLocation
new(@primitives().point)(anchorLocation[0], anchorLocation[1])
_should_close_infowindow: ->
@internal_options.singleInfowindow and @constructor.CURRENT_INFOWINDOW?
_randomized_coordinates: ->
return [@args.lat, @args.lng] unless _.isNumber(@internal_options.maxRandomDistance)
#gives a value between -1 and 1
random = -> (Math.random() * 2 - 1)
dx = @internal_options.maxRandomDistance * random()
dy = @internal_options.maxRandomDistance * random()
Lat = parseFloat(@args.lat) + (180/Math.PI)*(dy/6378137)
Lng = parseFloat(@args.lng) + ( 90/Math.PI)*(dx/6378137)/Math.cos(@args.lat)
return [Lat, Lng]