0

如何在javascript中将纬度和经度值转换为地址?我曾尝试过地理定位,但它只显示 10 个地址。它不会显示超过十个地址。我的代码:

                        if (result.Status.code == G_GEO_SUCCESS) {

                            for ( var i = 0; i < result.Placemark.length; i++) {

                                var addres = result.Placemark[i].address;
                                // var t1=setTimeout(function(){x.value="2 seconds"},2000);
                                if (addres == "") {
                                    // alert('blank');
                                    document.getElementById("msg" + noofid).innerHTML = "Unable to find location";
                                } else {
                                    document.getElementById("msg" + noofid).innerHTML = result.Placemark[i].address;

                                }
                                //alert("address");
                            }

                        }
                        // ====== Decode the error status ======
                        else {
                            var reason = "Code " + result.Status.code;
                            if (reasons[result.Status.code]) {
                                reason = reasons[result.Status.code];
                            }
                            alert('Could not find "' + search + '" '
                                    + reason);
                        }
                    });

}
4

1 回答 1

0

你好,因为你有一个关于这个 api 的具体问题,我遇到了你的问题,我将与你分享一些来自我的一个有类似需求的旧项目的资源。

// get nearest coords based on given tracking
tb.prototype.getNearestStreetCoords = function(lat, lng, callback) {

    var parentClass = this;

    var nearestStreet = {
        latitude: lat,
        longitude: lng
    };

    if (parentClass.useNearest != true) callback(nearest); 

    var request = {
        origin: new google.maps.LatLng(lat, lng), 
        destination: new google.maps.LatLng(lat, lng),
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService = new google.maps.DirectionsService();

    directionsService.route(request, function(response, status) {

        if (status == google.maps.DirectionsStatus.OK) {       
            callback(response);
        } else {
            callback(false);
        }

    });     
}


// get nearest address based on lat & lng
tb.prototype.getAddress = function(lat, lng, callback) {

    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'latLng': new google.maps.LatLng(lat, lng)}, function(results, status) {

        var streetAddress = null;

        if (status == google.maps.GeocoderStatus.OK) {
            streetAddress = results[0].formatted_address;
        } 

        callback(streetAddress);
    }); 
}
于 2013-09-05T10:57:00.853 回答