【问题标题】:Two Markers on Google Map with only one function call谷歌地图上的两个标记只有一个函数调用
【发布时间】:2018-04-10 10:49:37
【问题描述】:

我有一个很小的表单,用户可以在其中输入邮政编码和街道的位置。

<form>
    <input  id="zipcode" placeholder="" type="tel" pattern="d*" />
    <input  id="street1" placeholder=""  />
</form>

在 JS 中,当输入离开时,我会启动脚本。

$("#street1")
.blur(function () {
    if ($("#street1")
        .val()
        .length != 0 && $("#zipcode")
            .val()
            .length != 0) {
        var body = $("html, body");
        body.stop()
            .animate({
                scrollTop: 350
            }, 500, 'swing', function () {
                var targetAddress = $("#zipcode")
                    .val() + "," + $("#street1")
                    .val();
                codeAddress(targetAddress);
            });
    }
});

function codeAddress(target) {
address = target;
geocoder2 = new google.maps.Geocoder();
geocoder2.geocode({
    'address': address
}, function (results, status) {
    if (status === 'OK') {
        map.setCenter(results[0].geometry.location);
        homemarker2 = new google.maps.Marker({
            position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()),
            map: map,
            icon: iconPosition,
            draggable: true,
            title: 'Fixed'
        });
    }
});
}

地图以正确的位置为中心,但该位置不仅设置了一个标记,而且设置了两个标记。你能帮我解决这个问题吗?

【问题讨论】:

  • 您输入的是哪些值?这不会发生在我身上。请参阅output.jsbin.com/xulemof(我在输入中输入了1israel)。
  • @MoshFeu 在你的例子中我输入了 "20095" "Hamburg" 并且它再次发生:) 在 Safari、Firefox 和 Chrome 中

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


【解决方案1】:

因为您在 htmlbody 上都绑定了事件。

删除逗号$("html, body") 将按预期工作,

$("#street1")
.blur(function () {
    if ($("#street1")
        .val()
        .length != 0 && $("#zipcode")
            .val()
            .length != 0) {
        var body = $("html body"); // here
        body.stop()
            .animate({
                scrollTop: 350
            }, 500, 'swing', function () {
                var targetAddress = $("#zipcode")
                    .val() + "," + $("#street1")
                    .val();
                codeAddress(targetAddress);
            });
    }
});

【讨论】:

  • 真丢脸!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-11-07
  • 1970-01-01
  • 2010-10-11
  • 2020-02-19
  • 1970-01-01
  • 2012-09-22
  • 2011-09-08
相关资源
最近更新 更多