将纬度和经度值转换为地址

时间:2013-09-05 10:41:01

标签: javascript

如何在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);
                        }
                    });

}

1 个答案:

答案 0 :(得分: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);
    }); 
}