141 lines
5.0 KiB
C#
141 lines
5.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml;
|
|||
|
|
|||
|
namespace SafeMobileLib
|
|||
|
{
|
|||
|
public class ReverseGeocoding
|
|||
|
{
|
|||
|
private String ReverseGeoOSM = "127.0.0.1";
|
|||
|
|
|||
|
public ReverseGeocoding(String ReverseGeoOSM)
|
|||
|
{
|
|||
|
this.ReverseGeoOSM = ReverseGeoOSM;
|
|||
|
|
|||
|
doc = new XmlDocument();
|
|||
|
}
|
|||
|
|
|||
|
private volatile XmlDocument doc = null;
|
|||
|
private volatile XmlNode element = null;
|
|||
|
private volatile String addr = "";
|
|||
|
|
|||
|
|
|||
|
public enum Provider { OSM, GOOGLE, ANY }
|
|||
|
|
|||
|
public string getAddressLATLNG(Double LAT, Double LNG)
|
|||
|
{
|
|||
|
return getLatLngFromProvider(LAT, LNG, Provider.ANY);
|
|||
|
}
|
|||
|
|
|||
|
public void getCoordinates(String address, out decimal LAT, out decimal LNG)
|
|||
|
{
|
|||
|
LNG = -181; LAT = -181;
|
|||
|
try
|
|||
|
{
|
|||
|
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false");
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/status");
|
|||
|
if (element.InnerText == "OK")
|
|||
|
{
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/result/geometry/location/lat");
|
|||
|
LAT = Convert.ToDecimal(element.InnerText);
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/result/geometry/location/lng");
|
|||
|
LNG = Convert.ToDecimal(element.InnerText);
|
|||
|
|
|||
|
Utils.WriteLine($"Coordinates calc from google for address {address} are [{LAT},{LNG}]");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Utils.WriteLine($"Unable to get coordinates from google [{address}]. Response is {element.InnerText}");
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Utils.WriteLine("Unable to do coordinates calc from google");
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public string getAddressLATLNG2(Double LAT, Double LNG)
|
|||
|
{
|
|||
|
return getLatLngFromProvider(LAT, LNG, Provider.ANY);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private string getLatLngFromProvider(double LAT, double LNG, Provider provider)
|
|||
|
{
|
|||
|
bool errorFound = false;
|
|||
|
string address = "";
|
|||
|
|
|||
|
if (provider == Provider.OSM || provider == Provider.ANY)
|
|||
|
{
|
|||
|
Utils.WriteLine($"Doing reverse geocoding through OSM for [{LAT}:{LNG}]");
|
|||
|
addr = "";
|
|||
|
|
|||
|
address = $"{ReverseGeoOSM}?format=xml&lat={(LAT.ToString()).Replace(',', '.')}&lon={(LNG.ToString()).Replace(',', '.')}&zoom=18&addressdetails=1";
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
doc.Load(address);
|
|||
|
element = doc.SelectSingleNode("//reversegeocode/result");
|
|||
|
addr = element?.InnerText.Replace("'", "`");
|
|||
|
if (addr == null)
|
|||
|
{
|
|||
|
element = doc.SelectSingleNode("//reversegeocode/error");
|
|||
|
addr = element?.InnerText.Replace("'", "`");
|
|||
|
}
|
|||
|
if (addr.Contains("Unable to geocode"))
|
|||
|
{
|
|||
|
addr = "N/A";
|
|||
|
errorFound = true;
|
|||
|
}
|
|||
|
if (addr.StartsWith("Fail"))
|
|||
|
{
|
|||
|
addr = "N/A";
|
|||
|
errorFound = true;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
errorFound = true;
|
|||
|
Utils.WriteLine($"Unable to do Reverse Geocoding from OSM [{LAT},{LNG}] @ {address}. Response is {ex.ToString()}");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (provider == Provider.GOOGLE || (provider == Provider.ANY && errorFound))
|
|||
|
{
|
|||
|
addr = "";
|
|||
|
try
|
|||
|
{
|
|||
|
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + (LAT.ToString()).Replace(',', '.') + "," + (LNG.ToString()).Replace(',', '.') + "&sensor=false");
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/status");
|
|||
|
if (element.InnerText == "OK")
|
|||
|
{
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
|
|||
|
addr = element.InnerText.Replace("'", "`");
|
|||
|
Utils.WriteLine($"Reverse Geocoding [{LAT},{LNG}] is {addr}");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
errorFound = true;
|
|||
|
Utils.WriteLine($"Unable to do Reverse Geocoding from google [{LAT},{LNG}]. Response is {element.InnerText}");
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
errorFound = true;
|
|||
|
Utils.WriteLine("Unable to do Reverse Geocoding from google");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
return addr;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|