Post

5 followers Follow
0
Avatar

Parsing Address

How can I get just the zip code out of a map field in calculations?

A.R.E.I.S Support

Please sign in to leave a comment.

11 comments

0
Avatar

Hello Faisal,

it depends a bit on the structure of the address. If you're addresses always have a country at the end, it will have a structure like this:
10469 5th Ave, New York, NY 10065, USA
or
Marienplatz 33, 80331 München, Germany

You can use this code:

var a = @Map.join().split(",");
a.[a.length -1].replace(/[a-zA-Z" "]/g,"")

The code works like this:
- convert the address to a string (.join())
- look for commas as separator and split the string into an array of substrings (USA: 4 arrays, Germany: 3 arrays; the 1st array = 0, 2nd = 1 etc.)
- count the arrays (.length) and get the last but one array (which contains the zip in both examples)
- replace all letters with "" (nothing) and leave only numerals.

If you addresses are mixed (some have a country at the end, some not) you can first check if the last array contains letters only. And if there are letters only (= address has a Country cause a Country name never contains a numeral), the zip is in the last but one array, if there a numerals, the address doesn't have a Country and the zip is in the last array.

var a = @Map.join().split(",")
var b = a[a.length-1]
b.match(/[0-9]/) ? a[a.length-1].replace(/[a-zA-Z" "]/g,"") : a[a.length-2].replace(/[a-zA-Z" "]/g,"")

Rainer
rg@delos-consulting.com

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

i want to check, if the country was added for each entry - but sometimes the state is also added, sometimes not, sometimes no street is added - sometimes the city name or street name is more than one word... - how to check for the country then? could you please implement a function to get access to each field..would make everything so easy...

it´s also not possible to make the country obligate - if you enter some adress details, podio is already happy with it - any solution also to this?

Christoph Dingler 0 votes
Comment actions Permalink
0
Avatar

Hi Christoph,

a real cumbersome and dirty workaround could be this:

Get the let part of the address (if a country is in the address it's always the last part).
Create an array with all country which a relevant for you. 
Then check if an element of this array (= one country) is equal to this last part. 

var lastEl = @location.join().split(", ");
var lastEl = lastEl[lastEl.length-1];
var CountryArr = ["USA","Denmark","Germany","France"];
var count = 0;
for(var i = 0; i < CountryArr.length; i++){
if(CountryArr[i] == lastEl){
++count
}};
count == 0 ? "Country missing" : "OK"

If a country (an array element) is equal to the last part of the address it counts 1, if not count = 0. If count == 0, "Country is missing" is shown in the calculation field. 

Keep in mind: for .split(", ") it's comma plus space; each country name must be written exactly like it's shown in the address, it must be in quotation marks and separated by a comma from the next one. The list (array) of countries can be as long as you want/need.  

Another way for just checking is to filter for "location:country  - not set" in App view.

Rainer

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Hi Rainer. Is the without-country code still good from your end? I'm getting a "Unexpected token [". Thanks as always.

Gus H. 0 votes
Comment actions Permalink
0
Avatar

Hi Gus, 

you 've copied my typo from my first posting in this question. Remove the dot behind the first a in the last line. Must be

a[a.length -1]

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Still no luck. Just getting the building number. I should be doing sth wrong......

Gus H. 0 votes
Comment actions Permalink
0
Avatar

Hi Gus,

in your first posting it seems that you want to get something from a location field in the same item. But in your second posting it's @all of ...
So the code can return another result as in posting. The code simply returns everything but the digits from the  element in an array. But the arrays are different in posting 1 and posting 2.
Please explain what you want to achieve.

Rainer 

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Hey Rainer. I'm simply trying to remove the country word from the address. I can either go from a simple google map address field or "@All of Address" item to the calculation. It has not been working for me. Thank you.

Gus H. 0 votes
Comment actions Permalink
0
Avatar

Hi Gus,

enter @Property Address.join() in a calculation field and you'll see that it consists of 4 parts divided by a comma.), if the country name is in the  location field, If the the counry is missing, there are 3 parts.

The following code returns always the first 3 parts:

var loc = @Property Address.join().split(",").slice(0,3).join() 

But be careful: There'sa  (known) bug how the calc field returns a location field, it depends on how the location data are entered. If they are entered via an excel import it can happen, that the order is mixed up. 

Rainer

 

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Ingenious. I removed the var and it hits the spot. Thank you Rainer.

Gus H. 0 votes
Comment actions Permalink