Search code examples
c#google-mapsxamarin.formsreverse-geocodingcompass-geolocation

Check for zipcode , latitud, longitud range - using TKCustomMap-Xamarin-Forms


I have a map that returns current address and location also geo coordinates. However, I would like to add a condition as follows : if address.zipcode is ( 05001 ||05009||05030 ) do something , if not display an alert . Zip Codes for the State of Vermont

Zip Code City Zip Code Map 05001 White River Junction 05009 White River Junction
05030

The problem I am facing is , it returned the full address as a string. Is there any way I could validate only using the zipcode? so far , I tried if (address.title .contains("05001")); {do this...} else{alert..} Unfortunately , this wont work because those numbers can represent other things as a house number , city name , state or country etc. but the condition does not guarantee that it is in the states.

Thanks in advance !


Solution

  • Unfortunately the reverse geolocalization it returns a List<string> which cannot provide the field to access the specific information.
    You can anyway do a dirty job reading the zip code splitting the string and getting the fourth element. (the zip code should be in this position, but you should double check)
    Something like that:

    var geo = new Geocoder ();
    string []x;
    
    var addresses = await geo.GetAddressesForPositionAsync (new Position (12.34516, -12.345678));
    x = addresses.FirstOrDefault ().Split (',');
    
    if(x[3].Contains("your zip code"))
        DisplayAlert("Alert");
    

    It's not that elegant/robust but should do the job