gpt4 book ai didi

angularjs - Ionic/Angular JS - 从反向地理定位中获取特定值

转载 作者:行者123 更新时间:2023-12-03 18:02:58 25 4
gpt4 key购买 nike

我正在使用 Ionic/Angular JS 构建一个应用程序。在特定选项卡中,该应用程序能够自动填写一些表单字段,如街道名称、街道编号等。我的问题是,在某些情况下,例如街道号码不可用和 data[0].address_components[0].long_name改为填充街道名称。

这是我使用的代码:

.controller('PetitionsCtrl', function($scope, $cordovaGeolocation, $cordovaCamera, $log, $ionicLoading, $http, $timeout, $compile, Upload) {


$scope.getLocation = function() {
$ionicLoading.show({
templateUrl: 'loading.html',
hideOnStateChange: true
});
var posOptions = {timeout: 15000, enableHighAccuracy: true};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
$scope.pLat = {value: $scope.lat};
$scope.pLng = {value: $scope.lng};

var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng($scope.lat, $scope.lng);
var request = {
latLng: latlng
};

geocoder.geocode(request, function(data, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (data[0] != null) {
$scope.$apply(function () {
$scope.pStreet = {value: data[0].address_components[1].long_name};
$scope.pNumber = {value: data[0].address_components[0].long_name};
$scope.pCity = {value: data[0].address_components[2].long_name};
$scope.pAddress = {value: data[0].formatted_address};
setTimeout(function () {
$ionicLoading.hide();
}, 500);
});
} else {
$log.log("Adresa indisponibila");
setTimeout(function () {
$ionicLoading.hide();
}, 500);
}
}
})
});
};
})

以下是来自 Google 的示例响应:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

如何更改我的代码,以便检查 "types"读取值之前的值,如果不可用则将其留空。更具体地说,而不是使用 data[0].address_components[0].long_name要获取街道号码,我想获取 if "types"淡水河谷是 "street_number" .

最佳答案

我所理解的是,如果类型值等于 street_number您想获取该值,否则您想将空作为值 ''

geocoder.geocode(request, function(data, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (data[0] != null) {
$scope.$apply(function () {
$scope.pStreet = {value: data[0].address_components[1].long_name};
$scope.pNumber = {value: data[0].address_components[0].types[0] === 'street_number' ? data[0].address_components[0].long_name : ''};
$scope.pCity = {value: data[0].address_components[2].long_name};
$scope.pAddress = {value: data[0].formatted_address};
setTimeout(function () {
$ionicLoading.hide();
}, 500);
});
} else {
$log.log("Adresa indisponibila");
setTimeout(function () {
$ionicLoading.hide();
}, 500);
}
}
})

关于angularjs - Ionic/Angular JS - 从反向地理定位中获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40728691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com