将Jquery拖放到Google Map V3

时间:2015-02-12 14:42:33

标签: javascript jquery google-maps google-maps-api-3 droppable

情景:

我希望能够将商品放到Google Map&同时使用了搜索框。 拖着#34;纽约" (ui.draggable.text())将可拖动项目列入谷歌地图,它将在好地图上拉起纽约中心。

结果:

它有效,但它涵盖原始的"地方搜索框"我完全失去了搜索框,因为它填充了一个新地图...不确定如何调整以下代码,以便我保存所有删除的项目+选项以删除并保留搜索框

地图上的Droppable 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 个答案:

答案 0 :(得分:0)

我不确定我是否清楚地理解了您的问题,但这是一个示例地图,您可以在其中拖动和放大删除位置项同时使用“地方搜索”框。

<强> 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