gpt4 book ai didi

javascript - 如何在 geonames 和 geocoder api 中动态生成边界框

转载 作者:行者123 更新时间:2023-11-28 01:42:31 24 4
gpt4 key购买 nike

你好,当用户在 map 中输入城市时,我正在尝试获取地震信息。现在我通过硬编码来制作边界框(geonames 地震 api 需要)。我想制作输入城市大小的边界框。我在 geocoder 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);
}
});

问题 - 这不会让我得到 geonames 需要的东北西南。有人知道怎么做吗?

我当前的代码

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].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();

fixed fiddle

fiddle with return message from geonames

fiddle with hard-coded data that shows markers

关于javascript - 如何在 geonames 和 geocoder api 中动态生成边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20713456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com