简体   繁体   English

谷歌地图-标记不会移动,直到我手动将其移动一次

[英]google maps - marker not moving until I move it manually once

I have a map on the page http://www.liquidlizard.net/users/trainer 我在页面http://www.liquidlizard.net/users/trainer上有一张地图

When a user enters a location or postcode in the UK, and clicks search or hits enter then the map is meant to move to that point, which it does but it's meant to take the circle and marker with it. 当用户输入英国的位置或邮政编码,然后点击搜索或点击进入时,地图即会移动到该点,但实际上是要带上圆圈和标记。 At the moment, only after you've moved the marker by dragging it with the mouse does it then respond properly when you perform a search. 目前,只有在通过鼠标拖动标记来移动标记后,标记才能在执行搜索时正确响应。

I think it's got something to do with my dragend function but I'm not too great with javascript. 我认为这与我的dragend函数有关,但是我对javascript不太满意。

Can anyone help me out? 谁能帮我吗?

var userLocation = "London";
var geocoder = new google.maps.Geocoder();
var circle;
var latLng;
var marker;
var latlng;
var styles = [
              {
                    stylers: [
                      { hue: "#00ffe6" },
                      { saturation: -20 }
                    ]
              }
            ];

function initialize()
{
    latlng = new google.maps.LatLng(51.71194988641549, -0.4821940203124768);

    $('#lattitude').val(51.71194988641549);
    $('#longitude').val(-0.4821940203124768);

    var myOptions = {
        zoom: 6,
        styles: styles,
        center: latlng,
        //panControl: true,
        zoomControl: true,
        scaleControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

    marker = new google.maps.Marker({
        map: map,
        draggable:true,
        position: latlng
    });


    google.maps.event.addListener(marker, 'dragend', function() {
        //circle.setVisible(false);
        var point = marker.getPosition();
        map.panTo(point);
        $('#lattitude').val(point.lat());
        $('#longitude').val(point.lng());
        drawCircle($("#miles").val()/0.0621371192*100);
    });
}


$(function(){
    $( "#slider" ).slider({
        slide: function(event, ui) {
            $("#map-numOf-miles").html(ui.value+' Miles');
            $("#miles").val(ui.value);
            drawCircle(ui.value/0.0621371192*100);
        }
    });
});

function drawCircle(radius) {

    if (circle != undefined)
        circle.setMap(null);

    newlatlong = new google.maps.LatLng(parseFloat($('#lattitude').val()), parseFloat($('#longitude').val()));

    var options = {
        strokeColor: '#d12d87',
        strokeOpacity: 0.35,
        strokeWeight: 1,
        fillColor: '#d12d87',
        fillOpacity: 0.35,
        map: map,
        center: newlatlong,
        radius: radius
    };

    circle = new google.maps.Circle(options);
}

function geocode(){
 geocoder.geocode({
        address: $('#postcodeViolet').val()+' UK'
    }, function(results, status){
        if(status == google.maps.GeocoderStatus.OK){
         var geoPoint = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
            map.setCenter(geoPoint);
            circle.setCenter(geoPoint);
            marker.setPosition(geoPoint);
           $('#lattitude').val(geoPoint.lat());
           $('#longitude').val(geoPoint.lng());
        } else{
            alert("Can not geolocate this address.");
        }
    });
}

Also geocode called: 也称为地址解析:

//User clicks to search location
$(".spyglassIcon").click(function() {
    geocode();
});

$('.formInputTextPostcode').bind("enterKey",function(e){
    $(".spyglassIcon").click();
});
$('.formInputTextPostcode').keyup(function(e){
    if(e.keyCode == 13)
    {
      $(this).trigger("enterKey");
    }
});

I get a javascript error: Uncaught TypeError: Cannot call method 'setCenter' of undefined trainer:108 我收到一个JavaScript错误:Uncaught TypeError:无法调用未定义教练的方法“ setCenter”:108

That is because you don't create the circle in the path that causes the issue, this should address that: 那是因为您没有在引起问题的路径中创建圆,这应该解决以下问题:

function geocode(){
 geocoder.geocode({
        address: $('#postcodeViolet').val()+' UK'
    }, function(results, status){
        if(status == google.maps.GeocoderStatus.OK){
         var geoPoint = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
            map.setCenter(geoPoint);
            if (!!circle && !!circle.setCenter) circle.setCenter(geoPoint);
            marker.setPosition(geoPoint);
           $('#lattitude').val(geoPoint.lat());
           $('#longitude').val(geoPoint.lng());
        } else{
            alert("Can not geolocate this address.");
        }
    });
}

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

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