0

以下代码呈现地图,但未在单击事件上运行回调。有任何想法吗?

google.maps.visualRefresh = true
url = 'http://localhost:3000/closeby'

getCloseBy = (pos) ->
    $.post url, {'center': pos}

initialize = ->
    mapOptions = 
        zoom: 15
        mapTypeId: google.maps.MapTypeId.ROADMAP
    map = new google.maps.Map document.getElementById('map-canvas'), mapOptions

    if navigator.geolocation
    navigator.geolocation.getCurrentPosition (position)->
        pos = new google.maps.LatLng position.coords.latitude, position.coords.longitude
        infowindow = new google.maps.InfoWindow
            map: map
            position: pos
        map.setCenter pos
    , ->
        handleNoGeolocation true        
    else 
    handleNoGeolocation false       

  handleNoGeolocation = (errorFlag) ->
        if errorFlag
            content = 'Geolocation failed'
        else
            content = 'Your browser does not support Geolocation'
        options = 
            map: map
            position: new google.maps.LatLng 60, 105
            content: content
        infowindow = new google.maps.InfoWindow options
        map.setCenter options.position

  placeMarker = (location) ->
    marker = new google.maps.Marker
        position: location
        map: map

  google.maps.event.addListener map, 'click', (event)->
    placeMarker event.latLng    

google.maps.event.addDomListener window, 'load', initialize
4

1 回答 1

0

看起来像一个范围问题。placeMarker在 之外,定义initialize在哪里。map尝试在函数placeMarker内部移动。initialize或者map作为参数传递给placeMarker.

更新:我没有使用 CoffeeScript,但是在内部渲染initialize(而不是通过map)应该可以工作:

        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        google.maps.event.addListener(map, 'click', function(event) {
            placeMarker(event.latLng);
        });

        var placeMarker = function (location) {
            var options = { position: location, map: map };
            var marker = new google.maps.Marker(options);
        };
于 2013-07-29T19:20:44.127 回答