Reputation: 92581
I have an address string E.g. "12 Swiss Ave, Gonville, Wanganui, New Zealand"
Given this address I need to work out if the address is within the North or South island of New Zealand.
If I pass the address to the google maps api the data I get back is below, unfortunately though it doesn't tell me what island it is in. So, how can I check this?
{
"results": [
{
"address_components": [
{
"long_name": "12",
"short_name": "12",
"types": [
"street_number"
]
},
{
"long_name": "Swiss Avenue",
"short_name": "Swiss Ave",
"types": [
"route"
]
},
{
"long_name": "Gonville",
"short_name": "Gonville",
"types": [
"sublocality",
"political"
]
},
{
"long_name": "Wanganui",
"short_name": "Wanganui",
"types": [
"locality",
"political"
]
},
{
"long_name": "Manawatu-Wanganui",
"short_name": "Manawatu-Wanganui",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "New Zealand",
"short_name": "NZ",
"types": [
"country",
"political"
]
},
{
"long_name": "4501",
"short_name": "4501",
"types": [
"postal_code"
]
}
],
"formatted_address": "12 Swiss Avenue, Gonville, Wanganui 4501, New Zealand",
"geometry": {
"location": {
"lat": -39.9444245,
"lng": 175.0239575
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": -39.9430755197085,
"lng": 175.0253064802915
},
"southwest": {
"lat": -39.9457734802915,
"lng": 175.0226085197085
}
}
},
"types": [
"street_address"
]
}
],
"status": "OK"
}
Upvotes: 4
Views: 3363
Reputation: 4574
Well using Google Map's API..
You can get the placeId of the north island of NZ from say Places Autocomplete, which is ChIJhXgO3F4Uam0RdyMcnMutKMo
Then use the geocode library to get the bounds, and use the contains function to test if another lat/lng are in these bounds.
const geocode = async placeId => {
const geocoder = new window.google.maps.Geocoder();
return new Promise((resolve, reject) => {
geocoder.geocode(
{
placeId
},
(results, status) => {
resolve(results[0]);
}
);
});
};
const placeDetails = await geocode('ChIJhXgO3F4Uam0RdyMcnMutKMo');
const somePlace = getPlaceDetails(someOtherPlaceId);
const isInNorthIsland = placeDetails.geometry.bounds.contains(somePlace.geometry.location);
Upvotes: 0
Reputation: 161334
One option would be to get the polygons for the two islands and do point in polygon analysis on the geographic location.
Example using Polygons defined by KML and geoxml3
Proof of concept (using geoxml3 and modified KML from gadm.org)
============ older option using fusion tables, no longer works =============
Example using FusionTables (note: no longer works, Fusion Layers was turned down December 2019)
Proof of concept (using the Natural Earth data set in Fusion Tables and a "state to island" map) (note: also no longer works)
Upvotes: 3