0

在谷歌地图中,​​当我拖动文本框中显示的标记lat()lng()值时。我想做这里:

每次运动的坐标都会改变

我的以下代码部分对我有用。但是,当我将标记拖到远处时,它可以工作,但距离很近 lat()并且lng()不会改变?

我该如何解决这个问题?

主要的js代码是:

// fill in the UI elements with new position data
            function update_ui(address, lat, lng) {
            $('#lat').val(lat);
            $('#lng').val(lng);
        }


 // move the marker to a new position, and center the map on it
        function update_map(geometry) {
            map.fitBounds(geometry.viewport)
            marker.setPosition(geometry.location)
        }

和地理编码:

geocoder.geocode(request, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                // Google geocoding has succeeded!
                if (results[0]) {
                    // Always update the UI elements with new location data
                    update_ui(results[0].formatted_address, results[0].geometry.location.lat(), results[0].geometry.location.lng())

                    // Only update the map (position marker and center map) if requested
                    if (update) { update_map(results[0].geometry) }
                } else {
                    // Geocoder status ok but no results!?
                    alert("Error occured. Please try again.");
                }
            } else {
                // Google Geocoding has failed. Two common reasons:
                //   * Address not recognised (e.g. search for 'zxxzcxczxcx')
                //   * Location doesn't map to address (e.g. click in middle of Atlantic)

                if (type == 'address') {
                    alert("Sorry! We couldn't find " + value + ". Try a different search term, or click the map.");
                } else {
                    alert("Sorry! We couldn't find " + value + ". Try a different search term, or click the map.");
                    update_ui('', value.lat(), val.lng())
                }
            };
        });

编辑:

当我很少拖动标记时,坐标不会改变,但经过一些移动它们会改变。这是jsfiddle:

http://jsfiddle.net/jhoonbey/wwdE9/

4

1 回答 1

1

问题是您正在通过地理编码器传递标记的位置。

地理编码器获取原始纬度/经度位置并尝试将其映射到地址。这可能是一条街道、一个城镇或一个地区。Google 地理编码器返回的结果包括地址文本以及该地址的位置。

您正在显示地理编码器返回的位置。当用户在潜在地址密度低的区域周围移动标记时,您将看到混叠。这意味着用户“选择”的各种原始位置将反向地理编码到地理编码器返回的相同地址。

因此,当您实际做的是显示地理编码地址的纬度/经度时,它看起来好像标记的纬度/经度没有改变。

请注意,您链接到的站点在显示位置信息时不会进行任何地理编码。

于 2013-07-11T12:59:26.983 回答