Let's Geocode with Here.com
Welcome to the World of Geocodes
Have you ever wanted to see if an address is real and if it is get the geocode for it? Geocode, what is that? Ok, let's start with defining what a geocode is and what it can be used for. The location of everything on Earth can be defined as a set of latitude and longitude (a sort of set of coordinates). So, a geocode is simply this set of latitude and longitude that represents a geographic entity. Now that we know this, what is it used for. Simple, if you want to store a location on a Google map, it is easiest to store the geocode for it. If you have the geocode you can use it to add a marker to the map or even center the map. Another use is when you have several geocodes, you can build a fence on a map and then check the fence to see if a point is within the boundary of the fence, but that is for another post on another day, today we simply want to get the geocode for an address.
In this post I am going to take you through an example of how I validate and address and get its geocode. I will be using the Here.com platform to geocode an address. I will be using C# and .Net Core 6 to talk to the geocode api that Here.com provides.
Here.com - Map Data and Tools for Enterprises
Access is Something we can't live without
https://geocode.search.hereapi.com/v1/
geocode
?q=Invalidenstr+117+Berlin
&apiKey={YOUR_API_KEY}
{"items":[{"title":"Invalidenstraße 117, 10115 Berlin, Deutschland","id":"here:af:streetsection:tVuvjJYhO86yd5jk1cmzNB:CgcIBCCf2912EAEaAzExNw","resultType":"houseNumber","houseNumberType":"PA","address":{"label":"Invalidenstraße 117, 10115 Berlin, Deutschland","countryCode":"DEU","countryName":"Deutschland","stateCode":"BE","state":"Berlin","countyCode":"B","county":"Berlin","city":"Berlin","district":"Mitte","street":"Invalidenstraße","postalCode":"10115","houseNumber":"117"},"position":{"lat":52.53041,"lng":13.38527},"access":[{"lat":52.53105,"lng":13.3848}],"mapView":{"west":13.38379,"south":52.52951,"east":13.38675,"north":52.53131},"scoring":{"queryScore":1.0,"fieldScore":{"city":1.0,"streets":[1.0],"houseNumber":1.0}}}]}
Now for the magic
Now to code. I have a .Net 6 Console Application already set up. I've added a class called HereGeoCode to it where I have pasted the classes from json2sharp. I changed the namespace to HereGeoCode.
using Newtonsoft.Json;
using RestSharp;
var client = new RestClient("https://geocode.search.hereapi.com");
var request = new RestRequest("v1/geocode?q=84404+Njjdd+Dunes+Trl+McKinney+TX+75070&apiKey=jnddtDKi68lNy75MGM9bjx4LGnKpR74x_cVYd8o8Wi8", Method.Get);
request.AddHeader("User-Agent", "Nothing");
var geoResult = client.Execute(request).Content;
var root = JsonConvert.DeserializeObject<HereGeoCode.Root>(geoResult);
namespace GeoCode
{
public class AddressValidation
{
public double geoLat { get; set; }
public double geoLon { get; set; }
public bool isSuspect { get; set; }
}
}
var result = new GeoCode.AddressValidation();
if (root == null || root.items.Count == 0)
{
result.isSuspect = true;
result.geoLat = 0.00;
result.geoLon = 0.00;
}
else
{
var item = root.items[0];
result.geoLat = item.position.lat;
result.geoLon = item.position.lng;
if (item.resultType == "houseNumber")
{
result.isSuspect = false;
}
else if (item.resultType == "street")
{
if (item.scoring.fieldScore.streets[0] < 0.5)
{
result.isSuspect = true;
}
else
{
result.isSuspect = false;
}
}
else {
result.isSuspect = true;
}
}
Comments
Post a Comment