Javascript 如何获得道路/街道名称而不是国家路线?
我正在使用google maps API成功地获取地理位置的地址,但它通常会将道路或街道的名称替换为国家路线。这是令人困惑的,因为我们不使用国道,而是使用道路/街道名称 我找不到一个选项来请求街道名称而不是州路线 以下是我尝试过的: 返回状态路由的当前方法Javascript 如何获得道路/街道名称而不是国家路线?,javascript,google-maps,Javascript,Google Maps,我正在使用google maps API成功地获取地理位置的地址,但它通常会将道路或街道的名称替换为国家路线。这是令人困惑的,因为我们不使用国道,而是使用道路/街道名称 我找不到一个选项来请求街道名称而不是州路线 以下是我尝试过的: 返回状态路由的当前方法 function getAddress() { navigator.geolocation.getCurrentPosition(function (location) { mrtlat = location.coor
function getAddress() {
navigator.geolocation.getCurrentPosition(function (location) {
mrtlat = location.coords.latitude;
mrtlong = location.coords.longitude;
varStore.mrtlatlng = mrtlat + "," + mrtlong;
varStore.url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + varStore.mrtlatlng + "&key= ";
$.getJSON(varStore.url, function (data) {
var address = data.results[0].formatted_address;
varStore.location = address;
console.log(varStore.location);
new Messi('Current Location detected is <br>' + varStore.mrtlatlng + '<br/>' + varStore.location, {
title: 'Approximate Location', modal: true,
buttons: [{ id: 0, label: 'Close', val: 'X' }]
});
});
});
}
我知道这是可能的,因为我可以在谷歌地图上输入街道名称,并返回正确的地址,而不必使用州路线
也正在为chrome编写。您能提供一个例子吗,因为我不明白问题是什么。让我们来看一下谷歌的示例地理编码请求:<代码>https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=key作为第一个结果返回
281美国纽约州布鲁克林贝德福德大街11211号
。我知道你想知道路线的名称,即“贝德福德大道”。您可以在检查地址组件[x]时得到它。类型[x]==“route”,这就是您的意思吗?实际上,我想不惜一切代价避免使用路线名称,并在道路上使用长名称。下面是一个示例:latlng=-37.818182,%20145.19384。在这个例子中,我们看到的是米查姆路,但它被称为州36号公路。这是第一项。有没有办法把路线号转换成路名?谢谢,我知道了。看起来,与澳大利亚Mitcham VIC 3132 Mitcham路361号相对应的坐标是“-37.818291145.193999”(地点idChIJeUUC4Dw51moRWj4QXQxbIRs
)。地理编码器确实返回了它的街道名称:我明白了,为什么当我使用“latlng=-37.818182,%20145.19384”时,它会返回361国道36号作为地址,但将标记移动到该街道地址会给我Mitcham路?当我使用我的坐标时,地址组件给出了“长路径名称”:“州路径36”,“短路径名称”:“州路径36”,“类型”:[“路径”]“你有没有建议我如何可靠地得到街道名称而不是路线号?对不起,我不确定我是否理解你的问题。您所在位置的坐标更接近州际公路36/Brunswick路,这就是为什么Geocoder返回这些坐标而不是Mitcham路。这些都是街道地址名称(路线)而不是数字(?)。
function getAddress3() {
navigator.geolocation.getCurrentPosition(function (location) {
varStore.mrtlatlng = location.coords.latitude + "," + location.coords.longitude;
varStore.url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + varStore.mrtlatlng + "&key= ";
$.getJSON(varStore.url, function (data) {
// The following block of code tests the first 3 for State route and will set the address to the first one that does not contain state route
// Needs to be cleaned up
var k = 0;
var foundValid = false;
var address = data.results[0].formatted_address;
while (k <3 && foundValid == false) {
if (data.results[k].formatted_address.includes("State Route")) {
if (data.results[k].address_components[1].long_name.includes("State Route") == false) {
}
k++;
} else {
foundValid = true;
address = data.results[k].formatted_address + "<br/> Found valid address";
console.log(data.results[k].address_components[0].short_name);
console.log(data.results[k].address_components[0].long_name);
console.log("true");
}
}
if (foundValid == true) {
//do nothing
} else {
address = data.results[0].formatted_address + "<br/> Could not find valid address";
address = data.results[0].address_components
varStore.location = address;
console.log(varStore.location);
new Messi('Current Location detected is <br>' + varStore.mrtlatlng + '<br/>' + varStore.location, {
title: 'Approximate Location', modal: true,
buttons: [{ id: 0, label: 'Close', val: 'X' }]
});
};
});
}
)}
varStore.url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + varStore.mrtlatlng + "&key= ";
$.getJSON(varStore.url, function (data) {
var address = "";
if (data.results[0].address_components[1].long_name.includes("State Route") == false) {
address = data.results[0].address_components[0].long_name + " "// house
+ data.results[0].address_components[1].long_name + " " //street
+ data.results[0].address_components[2].long_name + " "//suburb
+ data.results[0].address_components[3].short_name + " " //state
+ data.results[0].address_components[5].short_name + " "//post
} else if (data.results[3].address_components[1].long_name.includes("State Route") == false) {
address = data.results[3].address_components[0] + " "// house
+ data.results[3].address_components[1].long_name + " " //street
+ data.results[3].address_components[2].long_name + " "//suburb
+ data.results[3].address_components[3].short_name + " " //state
+ data.results[3].address_components[5].short_name + " "//post
} else if (data.results[2].address_components[1].long_name.includes("State Route") == false) {
address = data.results[2].address_components[0] + " "// house
+ data.results[2].address_components[1].long_name + " " //street
+ data.results[2].address_components[2].long_name + " "//suburb
+ data.results[2].address_components[3].short_name + " " //state
+ data.results[2].address_components[5].short_name + " "//post
} else {
address = data.results[0].address_components[0] + " "// house
+ data.results[0].address_components[1].long_name + " " //street
+ data.results[0].address_components[2].long_name + " "//suburb
+ data.results[0].address_components[3].short_name + " " //state
+ data.results[0].address_components[5].short_name + " "//post
}
//var address = data.results[0].formatted_address;
varStore.location = address;
console.log(varStore.location);