【发布时间】:2023-06-26 22:00:01
【问题描述】:
好的,这个问题是2折:
- 我想在 google 地图上显示一个显示县的叠加层...我该怎么做?
- 我想在动态创建标记时根据 lat/lng 获取县。
我认为问题 2 涉及更多。这是我到目前为止的代码。基本上......“工作标记”以 3 种方式创建: 一个。地址中的用户类型---地址被地理编码,标记被设置 湾。用户双击地图并设置标记---地址被反向地理编码 3.用户将默认标记拖到地图上的新区域---标记集----地址被反向地理编码。
代码如下:
initialize();
//Function for the autocomplete field for map
$(function() {
$("#address").autocomplete({
//This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term + ', us' }, function(results, status) {
response($.map(results, function(item) {
return {
label: item.formatted_address,
value: item.formatted_address,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
}
}));
})
},
//This bit is executed upon selection of an address
select: function(event, ui) {
$("#latitude").val(ui.item.latitude);
$("#longitude").val(ui.item.longitude);
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
jobMarker.setPosition(location);
map.setCenter(location);
}
});
});
//when user drags job marker the new info is added.
google.maps.event.addListener(jobMarker, 'drag', function() {
geocoder.geocode({'latLng': jobMarker.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$('#address').val(results[0].formatted_address);
$('#latitude').val(jobMarker.getPosition().lat());
$('#longitude').val(jobMarker.getPosition().lng());
}
}
});
});
google.maps.event.addListener(map, "dblclick", function(event)
{
// display the lat/lng in your form's lat/lng fields
var location = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
jobMarker.setPosition(location);
geocoder.geocode({'latLng': jobMarker.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$('#address').val(results[0].formatted_address);
$('#latitude').val(jobMarker.getPosition().lat());
$('#longitude').val(jobMarker.getPosition().lng());
}
}
});
});
【问题讨论】:
标签: javascript jquery google-maps