有没有办法做到这一点,以便在使用谷歌地图 API 并搜索类型为"APPROXE"的位置时,它会自动缩小并以红色突出显示大致区域 - 确切地说,它将如何在谷歌上工作 - 使用谷歌地图 API v3? https://i.stack.imgur.com/9vHS7.png
现在在我的网站上,当您搜索澳大利亚时,它会将查询到北领地,并且只是将地图集中在一个随机点上。我正在获取用户输入的地址并转换为纬度/液化天然气,然后将其发送到谷歌地图 api。(截图:https://i.stack.imgur.com/uQvAR.png)请告知是否有其他方法,谢谢。
我像这样声明地图画布:
map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(<?php echo $geo_latitude; ?>, <?php echo $geo_longitude; ?>),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
<?php echo(isMobile()) ? 'draggable: false' : ''; ?>
});
这是将用户写入的位置地址解析为纬度/经度的函数。
// convert address to lat/long
function get_lat_long($address){
$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
$types = $json->{'results'}[0]->{'types'};
$location_type = $json->{'results'}[0]->{'geometry'}->{'location_type'};
if ( $types[0] == 'administrative_area_level_1' && $types[1] == 'political') {
$types = "STATE";
} elseif ( $types[0] == 'country' && $types[1] == 'political' ) {
$types = "COUNTRY";
} else {
$types = "OTHER";
}
return $lat.','.$long.','.$types.','.$location_type;
}
如果您使用 Google Maps Javascript API v3 Geocoder,它可以在结果中包含视口和/或边界属性,允许您缩放地图,使其边界完全包含结果。
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
if (results[0].geometry.viewport)
map.fitBounds(results[0].geometry.viewport);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});