如何从JSON数据合并两个不同的对象

时间:2017-02-07 02:04:57

标签: angularjs arrays json concatenation

您好我需要一些有关此代码段的帮助。我得到的输出是JSON数据的多个不同对象。但我想提取目标数据然后将它们合并为一个数组。但我获得的所有数据只是一个数据合并为一个数组,一个接一个。 输出结果如下:enter image description here

$scope.findHospital = function(){

    $ionicLoading.show({
        template: '<ion-spinner icon="bubbles"></ion-spinner><br/>Acquiring location!'
    });

    var posOptions = {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 0
    };

    $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
        var lat  = position.coords.latitude;
        var long = position.coords.longitude;
        var infowindow;

        var myLatlng = new google.maps.LatLng(lat, long);

        var mapOptions = {
            center: myLatlng,
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map"), mapOptions);
        infowindow = new google.maps.InfoWindow();

        var service = new google.maps.places.PlacesService(map);
        service.nearbySearch({
            location: myLatlng,
            radius: 5000,
            type: ['police']
        }, callback);

        function callback(results, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
                for (var i = 0; i < results.length; i++) {
                    createMarker(results[i]);
                }
            }
        }

        function createMarker(place) {
            geocoder = new google.maps.Geocoder();

            var latlng = new google.maps.LatLng(lat, long);
            geocoder.geocode({'latLng': latlng}, function(results, status) {
                var placeLoc = place.geometry.location;
                var marker = new google.maps.Marker({
                  map: map,
                  position: place.geometry.location
                });
                var request = { reference: place.reference };

                service.getDetails(request, function(details, status) {
                    console.log(details);

                    var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+results[0].formatted_address+"&destinations="+details.formatted_address+"&key=AIzaSyDDxMGpu5SP5XwZGrKw5BQWl_r2dgLgpPY";
                    var distances = [];

                    $http.get(url).then(function(response){
                        var str = response.data.rows[0].elements[0].distance.text;

                        distances = distances.concat(str);

                        console.log(distances);
                    });
                }
            });
        }
    }
}

CODE UPDATED!

2 个答案:

答案 0 :(得分:0)

这就是你需要的!!函数createMarker之外的数组。当您致电$http.get时,您应该将响应与distances数组连接起来。

var distances = []; // HERE

function createMarker(place) {
    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(lat, long);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });
        var request = { reference: place.reference };

        service.getDetails(request, function(details, status) {

            var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+results[0].formatted_address+"&destinations="+details.formatted_address+"&key=AIzaSyDDxMGpu5SP5XwZGrKw5BQWl_r2dgLgpPY";

            $http.get(url).then(function(response){
                var str = response.data.rows[0].elements[0].distance.text;

                distances = distances.concat(str); // HERE
            });
        }
    });
}

答案 1 :(得分:0)

我强烈怀疑,每次调用distances方法时,createMarker数组都在初始化。因此,将您的distances置于全局范围内。即刚刚解决getCurrentPosition承诺。

var distances = [];
$cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
// your code

并从内部代码中删除距离初始化。