你好我想在用户将城市输入地图时获取地震信息。现在我通过硬编码制作边界框(geonames earthquake api所需)。我想制作进入城市大小的边界框。我在地理编码器api v3中看到这样做:
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.fitBounds(results[0].geometry.viewport);
}
});
问题 - 这不会让我得到东北西南的地理名称需要。有谁知道怎么样?
我当前的代码
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Calculate bounding box based on the given address
var north = results[0].geometry.location.lat() + .5;
var south = results[0].geometry.location.lat() - .5;
var east = results[0].geometry.location.lng() + .5;
var west = results[0].geometry.location.lng() - .5;
var earthquake = 'http://api.geonames.org/earthquakesJSON?north=' + north + '&south=' + south + '&east=' + east + '&west=' + west + '&username=*****';
+5 -5是扩展坐标中的数字以创建南北......等等。
答案 0 :(得分:1)
你想使用结果[0] .geometry.viewport这是一个google.maps.LatLngBounds object
var north = results[0].geometry.viewport.getNorthEast().lat();
var south = results[0].geometry.viewport.getSouthWest().lat();
var east = results[0].geometry.viewport.getNorthEast().lng();
var west = results[0].geometry.viewport.getSouthWest().lng();