简体   繁体   English

无法使用JavaScript在Google Map中正确拖动标记

[英]Cannot drag marker properly in Google Map using JavaScript

I cannot drag marker of Google Map using JavaScript. 我无法使用JavaScript拖动Google Map的标记。 Here is my code: 这是我的代码:

geocoder.geocode( { 'address': addrs}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        $('#latitude').val(results[0].geometry.location.lat());
        $('#longitude').val(results[0].geometry.location.lng());
        var latitude=results[0].geometry.location.lat();
        var longitude=results[0].geometry.location.lng();
        var title=document.getElementById('googleMap').value;
        var desc=title+","+address;
        console.log(latitude,longitude);
        var markers = [{"lat":latitude,"lng":longitude},{"title": title,"lat":latitude,"lng":longitude,"description":desc}];
    // $window.addEventListener('load', onload, false);
     // $window.onload = function () {
    setTimeout(function(){
        var mapOptions = {
         center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
                google.maps.event.trigger(map, "resize");
                var infoWindow = new google.maps.InfoWindow();
                var lat_lng = new Array();
                var latlngbounds = new google.maps.LatLngBounds();
                for (i = 0; i < markers.length; i++) {
                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                lat_lng.push(myLatlng);
                var marker = new google.maps.Marker({
                draggable: true,
                position: myLatlng,
                map: map,
                title: data.title
                });
                latlngbounds.extend(marker.position);
                (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
                });
                })(marker, data);
                google.maps.event.addListener(marker, 'dragend', function (event) {
                    var latdrag = this.getPosition().lat();
                    var longdrag= this.getPosition().lng();
                    console.log('latlong',latdrag,longdrag);
                });
                }
                map.setCenter(latlngbounds.getCenter());
                map.fitBounds(latlngbounds);
                zoomChangeBoundsListener = 
                google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
if (this.getZoom()){
    this.setZoom(12);
}
});
 setTimeout(function(){google.maps.event.removeListener(zoomChangeBoundsListener)}, 2000);
},2000);
} 
else {
    alert("Geocode was not successful for the following reason: " + status);
              }
});

Here my issue is I am able to drag the marker but while I am dragging the new marker is coming to droppable place and the first one is still there. 在这里,我的问题是我能够拖动标记,但是当我拖动新标记时,新标记即将到达可放置的位置,而第一个标记仍在那儿。 Here I need that single marker will only drag to any place and no new marker will create. 在这里,我需要单个标记只能拖到任何位置,并且不会创建新标记。

You are creating two markers in the same place and dragging the top one. 您要在同一位置创建两个标记,然后拖动最上面的一个。

var markers = [{"lat":latitude,"lng":longitude},{"title": title,"lat":latitude,"lng":longitude,"description":desc}]; var markers = [{{“ lat”:latitude,“ lng”:longitude},{“ title”:标题,“ lat”:纬度,“ lng”:经度,“ description”:desc}];

creates an array with two elements and in the loop, two markers are created. 创建一个包含两个元素的数组,并在循环中创建两个标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM