如何找到街道起点和终点的纬度和经度?

时间:2019-06-11 12:34:05

标签: google-maps

我正在尝试使用GOOGLE MAPS API查找从街道的起点到终点的街道坐标(纬度)。我在网上找不到一些simillaria

谢谢

我使用以下代码:

function findEndAddr(addr) {
        for (var i = 500;;) {
var Http = new XMLHttpRequest();
                                  var url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + i.toString().split('.')[0] + addr.replace(/\s/g, '') + '&key=KEY_API';
                                  console.log(url);
            Http.open("GET", url, false);
            Http.send(null);
            if (Http.status == 200 && Http.readyState == 4) {
                var resp = JSON.parse(Http.responseText);
                if (resp['results'][0]['address_components'][0]['types'][0] != 'street_number') {
                    i = i / 2;
                }
                else {
                    return findEndAddrNext(addr, i, i, 2);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我使用此功能:

function findEndAddrNext(addr, i, nb, div) {
        var Http = new XMLHttpRequest();
        var url;
    url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + nb.toString().split('.')[0] + addr.replace(/\s/g, '') + '&key=KEY-API';
    console.log(url);
        Http.open("GET", url, false);
        Http.send(null);
        if (Http.status == 200 && Http.readyState == 4) {
    var resp = JSON.parse(Http.responseText);
    var index = 0;
    for (let cpt = 0; cpt < resp['results'].length; cpt++) {
                            console.log(getThing(resp['results'][cpt]['address_components'], 'postal_code'));
                            console.log(addr.split(',')[2]);
                            if (getThing(resp['results'][cpt]['address_components'], 'locality') == addr.split(',')[1]) {
                            index = cpt;
                            break;
                            }
                            }
            if (resp['results'][index]['address_components'][0]['types'][0] == 'street_number') {
                i = nb;
                            if ((nb / div) <= 2) {
                                              if (getThing(resp['results'][index]['address_components'], 'locality') != addr.split(',')[1]) {
                                              oui = searchFinal(getThing(resp['results'][index]['address_components'], 'street_number'), addr);
                                              return;
                            }

                    oui = [resp['results'][index]['geometry']['location'].lat, resp['results'][0]['geometry']['location'].lng];
                    console.log(resp);
                    return;
                }
                nb = nb + (nb / div);
                findEndAddrNext(addr, i, nb, div);
            }
            if (resp['results'][index]['address_components'][0]['types'][0] != 'street_number') {
                div *= 2;
                nb = i + (i / div);
                findEndAddrNext(addr, i, nb, div);
            }
        }
                                  }