代码之家  ›  专栏  ›  技术社区  ›  MilkACow

将Jquery拖放到Google Map V3

  •  -1
  • MilkACow  · 技术社区  · 10 年前

    脚本:

    我希望能够将项目拖放到Google Map&同时使用了搜索框。 将“New York”(ui.draggable.text())列出的可拖动项目拖到谷歌地图中,它将在好的地图中拉出纽约中心。

    结果:

    它可以工作,但它覆盖了原始的“地点搜索框”,我完全失去了搜索框,因为它填充了一个新地图。。。不知道如何调整下面的代码,所以我保存了所有删除的项目+选项,以删除并保留搜索框

    地图上可拖放的div:

    $('.dropit').droppable({
            drop: function(event, ui) {
    
                geocoder = new google.maps.Geocoder();
                var address = ui.draggable.text();
                geocoder.geocode( { 'address': address}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                    } else {
                      alert('Geocode was not successful for the following reason: ' + status);
                    }
                });
    
                var mapOptions = {
                   // center:  ui.draggable.text().geometry.location(),
                    zoom: 8,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
    
                var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                //ui.draggable.text() DISPLAY THIS ON MAP
            }
        });
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   MrUpsidown    10 年前

    我不太清楚你的问题,但这里有一个示例地图,你可以在上面拖动&放置位置项目 同时使用“地点搜索”框。

    HTML格式 :

    <div id="map-canvas"></div>
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <ul>
        <li>New York, USA</li>
        <li>Shanghai, PRC</li>
        <li>London, UK</li>
        <li>Paris, France</li>
        <li>Sydney, Australia</li>
    </ul>
    

    JavaScript (拖放):

    $("ul li").each(function () {
        $(this).draggable({
            cursor: 'move',
            revert: true
        });
    });
    
    $('#map-canvas').droppable({
    
        drop: function (event, ui) {
    
            var el = ui.draggable;
    
            codeAddress(el.text());
            el.remove();
        }
    });
    

    JavaScript (地点搜索):

    function codeAddress(address) {
    
        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
    

    下面是一个工作演示。

    JSFiddle demo

    推荐文章