angularjs – Ionic / Angular JS – 从Reverse Geolocation获取
发布时间:2020-12-17 18:03:03 所属栏目:安全 来源:网络整理
导读:我正在使用Ionic / Angular JS构建一个App.在特定标签中,应用程序可以自动填写某些表单字段,如街道名称,街道号码等.我的问题是,在某些情况下,街道号码不可用,数据[0] .address_components [0 ] .long_name填充街道名称. 这是我使用的代码: .controller('Pet
我正在使用Ionic / Angular JS构建一个App.在特定标签中,应用程序可以自动填写某些表单字段,如街道名称,街道号码等.我的问题是,在某些情况下,街道号码不可用,数据[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",{ "long_name" : "California","short_name" : "CA","types" : [ "administrative_area_level_1",{ "long_name" : "United States","short_name" : "US","types" : [ "country",{ "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" } 如何更改我的代码,以便在读取值之前检查“类型”值,如果不可用则将其留空.为了更加特别,而不是使用数据[0] .address_components [0] .long_name来获取街道号码,我想知道“类型”值是“street_number”. 解决方法
我所理解的是,如果类型值等于street_number,你想获得值,否则你想把空值作为值”
geocoder.geocode(request,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); } } }) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- webservice [META-INF/xfire/services.xml] cannot be open
- scala – 从字符串到数字类型的快速安全转换
- 在angular 2材质中设置datepicker的默认日期
- scala – 什么是适当的monad或序列理解来映射和携带状态?
- Angular2部署在Tomcat总结
- 【AngularJS】4.AngularJS $scope里面的$apply方法和$watch
- scala – 隐式添加对象方法
- vim – 如何在NERDTree插件中刷新
- Scala解析器组合器和换行符分隔文本
- bash – 如何在Intoserver上以编程方式在Geoserver上设置用