2016-11-21 21 views
5

से विशिष्ट मान प्राप्त करें मैं आयनिक/कोणीय जेएस का उपयोग कर एक ऐप बना रहा हूं। एक विशिष्ट टैब में, ऐप में स्ट्रीट नेम, स्ट्रीट नंबर इत्यादि जैसे स्वचालित रूप से कुछ फॉर्म फ़ील्ड भरने की क्षमता होती है। मेरी समस्या यह है कि, कुछ स्थितियों में उदाहरण के लिए स्ट्रीट नंबर उपलब्ध नहीं है और 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); 
      } 
      } 
     }) 
     }); 
    }; 
}) 

यहाँ गूगल से एक उदाहरण प्रतिक्रिया है::

यहाँ कोड है कि मैं का उपयोग करें

{ 
    "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 का उपयोग करने के बजाय, मैं "types" vale "street_number" प्राप्त करना चाहता हूं।

उत्तर

0

मैं क्या समझ चुके हैं कि अगर प्रकार मूल्य 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); 
      } 
      } 
     }) 
+0

की तरह किसी भी अन्य मूल्य खोजने के लिए इस कोड का उपयोग कर सकते मुझे अपने आवेदन में भौगोलिक स्थान पर जाना है। जब भी मैं विशेष पृष्ठ पर जाऊंगा तो पॉप अप को भौगोलिक स्थान या से इनकार करने के लिए आना चाहिए ताकि यह कोड उस के लिए उपयोगी हो या आप मुझे सुझाव दे सकें कि इस पॉपअप को भौगोलिक स्थान की अनुमति देने के लिए कैसे जोड़ा जाए .. –

+0

जैसा कि मैंने अपने उत्तर में कहा है, सही नहीं है क्योंकि आप सरणी पर केवल पहले तत्व की जांच कर रहे हैं। – Jorge

-1

Ryad.iv जवाब सिर्फ पहली जाँच कर रहा है के रूप में खाली रखना चाहते है सरणी में स्ट्रिंग। इसके लिए ठीक से काम करने के लिए आपको सरणी के सभी तत्वों को जांचना होगा।

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.indexOf('street_number') !== -1 ? 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); 
      } 
      } 
     }) 

यह जांचने का यह अच्छा तरीका है। दूसरे उत्तर के साथ, यदि street_name सरणी की दूसरी स्थिति है तो आप इसे सही तरीके से सम्मिलित नहीं करेंगे।

आशा है कि यह

+0

आप अनुमान लगा सकते हैं कि 'street_number' वाले प्रकारों की सरणी हमेशा पहली स्थिति में' street_number' के साथ एक एकल तत्व सरणी होगी। – Jorge

+0

street_number के मामले में यदि उसके पास एक से अधिक मूल्य हैं तो इसका मतलब है कि यह सिर्फ street_number नहीं हो सकता है। –

+0

आप उसे नहीं जानते हैं। और यदि आज ऐसा है कि Google कैसे करता है और कल यह आपके ऐप को तोड़ने के कार्यान्वयन को बदल देता है। इसे इस तरह से बनाने के लिए बेवकूफ है और मुझे खेद है लेकिन यह एक बहुत बुरा जवाब है। मुझे यकीन है कि आपने मेरा जवाब कम कर दिया है, लेकिन विश्वास करो या सही नहीं है। – Jorge

0

आप की तरह एक मैच खोजने के लिए सेट परिणाम पर पाश करने के लिए है में मदद करता है:

Angular.forEach(data[0].address_components, function(val, index){ 
    if (val.type[0]=='street_number') { 
     $scope.pNumber = {value: val.long_name}; 
    } 
}); 

आप city या country आदि

संबंधित मुद्दे