Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

 Subscribe to receive our newsletter. Subscribe Unsubscribe

## Getting street from latitude/longitude?

This is a discussion on Getting street from latitude/longitude? within the MapPoint Desktop Discussion forums, part of the Map Forums category; Hello, I have the latitude and longitude of a position. I need to get the nearest street for that point. ...

1. Member Yellow Belt
Join Date
Feb 2005
Posts
32

## Getting street from latitude/longitude?

Hello,
I have the latitude and longitude of a position. I need to get the nearest street for that point. Is this called reverse geocoding?
Does anyone have an C# example how to do that? I only found VisualBasic examples.
Which algorithm gives me the best result?

Bye,

MD

2. Senior Member Black Belt
Join Date
Nov 2004
Location
Belgium
Posts
2,433
Hi,

This gets you the streetaddress where you are on. But oif course not the nearest one if you are not on a street. If you need that you have to make a circle, take a radius of lets say 5 meter, then calc position every 5 meter on the circumfence of the circle. If not found then increase radius with 5 meter and do the same on the new circumfence. Of course you have to stop on a given maxium. Be aware that involved people must know you make an assumption with this !

Code:
```            Map map = MP.ActiveMap;
Location Loc = map.GetLocation&#40;Lat, Lon, 1&#41;;
FindResults StreetResults = map.ObjectsFromPoint&#40;map.LocationToX&#40;Loc&#41;, map.LocationToY&#40;Loc&#41;&#41;;

foreach &#40;object o in StreetResults&#41; &#123;
Location StreetLoc = o as Location;
if&#40;StreetLoc != null && StreetLoc.StreetAddress != null&#41; &#123;
Console.WriteLine&#40;"value    " + StreetLoc.StreetAddress.Value&#41;;
Console.WriteLine&#40;"street   " + StreetLoc.StreetAddress.Street&#41;;
Console.WriteLine&#40;"postcode " + StreetLoc.StreetAddress.PostalCode&#41;;
Console.WriteLine&#40;"city     " + StreetLoc.StreetAddress.City&#41;;
&#125;
&#125;```

3. Member Yellow Belt
Join Date
Feb 2005
Posts
32
Hello,
thanks for your reply. There are two things, which I dont understand.

I use the point (Lat: 48,37017; Long: 10,8903).

1. Why does the ObjectsFromPoint function has as a result serveral locations?
Some of the result locations have a StreetAddress. None of them have a city.
The street are "A7", "A95", but the shouldnt be next to the given location.

2. How can I make a circle around a position to find the nearest street?

Do I have to use the goto function to get proper results?

Bye,

MD

4. Member Yellow Belt
Join Date
Feb 2005
Posts
32
Hello,
i found out that you need to use the goto() method, so that it will work.

I still have no idea how to make a circle to get the nearest street. Could you help me?

Bye,

MD

5. Member Yellow Belt
Join Date
Feb 2005
Posts
32
Hello,
I tried to use the code of one of the articles about Reverse Geocoding in Delphi and tried to convert it to C#:

Code:
```				// First, try to find real latitude/longitude
Location Loc = map.GetLocation&#40;latitude, longitude, 1&#41;;
Loc.GoTo&#40;&#41;;
FindResults StreetResults = map.ObjectsFromPoint&#40;map.LocationToX&#40;Loc&#41;, map.LocationToY&#40;Loc&#41;&#41;;

Boolean foundStreet = false;
foreach &#40;object o in StreetResults&#41;
&#123;
if &#40;o is Location&#41;
&#123;
Location StreetLoc = o as Location;
if&#40;StreetLoc != null && StreetLoc.StreetAddress != null&#41;
&#123;
text += "\"" + dateTime + "\",\"" + StreetLoc.StreetAddress.PostalCode + "\",\"" + StreetLoc.StreetAddress.City + "\",\"" + StreetLoc.StreetAddress.Street + "\",\"" + latitude + "\",\"" + longitude + "\"\r\n";
foundStreet = true;
break;
&#125;
&#125;
&#125;

// no street found ==> search around in circles
if &#40;!foundStreet&#41;
&#123;
double maxDistance = 0.4;
double distance = 0.05;
while &#40;distance <= maxDistance&#41;
&#123;
double radius = 1 / 60 / 1852 * distance;
double circum = Math.Floor&#40;distance * 2 * Math.PI&#41;;
double count =  circum / 5;
double angleInc = 360 / count;
if &#40;angleInc <= 0&#41;
&#123;
foundStreet = false;
break;
&#125;
double angle = 0;

while &#40;angle < 360&#41;
&#123;
double lat = calcLat&#40;latitude, longitude, angle, radius&#41;;
double lon = calcLon&#40;latitude, longitude, angle, radius&#41;;

Loc = map.GetLocation&#40;lat, lon, 1&#41;;
Loc.GoTo&#40;&#41;;
StreetResults = map.ObjectsFromPoint&#40;map.LocationToX&#40;Loc&#41;, map.LocationToY&#40;Loc&#41;&#41;;

// Search with new coordinates
foreach &#40;object o in StreetResults&#41;
&#123;
if &#40;o is Location&#41;
&#123;
Location StreetLoc = o as Location;
if&#40;StreetLoc != null && StreetLoc.StreetAddress != null&#41;
&#123;
text += "\"" + dateTime + "\",\"" + StreetLoc.StreetAddress.PostalCode + "\",\"" + StreetLoc.StreetAddress.City + "\",\"" + StreetLoc.StreetAddress.Street + "\",\"" + latitude + "\",\"" + longitude + "\"\r\n";
foundStreet = true;
break;
&#125;
&#125;
&#125;

if &#40;foundStreet&#41;
break;
angle = angle + angleInc;
&#125;
if &#40;foundStreet&#41;
&#123;
break;
&#125;
distance = distance + 0.05;
&#125;
&#125;

if &#40;!foundStreet&#41;
&#123;
text += "\"" + dateTime + "\",\"---\",\"---\",\"---\",\"" + latitude + "\",\"" + longitude + "\"\r\n";
&#125;
&#125;

private static double calcLon &#40;double latitude, double longitude, double angle, double radius&#41;
&#123;
double r = angle * Math.PI / 180;
double c = radius;
double b = c * Math.Cos&#40;r&#41; / Math.Cos&#40;&#40;latitude * Math.PI / 180&#41;&#41;;
return longitude + b;

&#125;

private static double calcLat &#40;double latitude, double longitude, double angle, double radius&#41;
&#123;
double r = angle * Math.PI / 180;
double c = radius;
double a = c * Math.Sin&#40;r&#41;;
return latitude + a;
&#125;```

Something must be wrong. Could anyone please help me, because I dont't get any street names, when the position is only close to a street.

Bye,

MD

6. Senior Member Black Belt
Join Date
Nov 2004
Location
Belgium
Posts
2,433
Hi,

I made the class in C3 for you. this is the code:

Code:
```    public class MPPosInCircle
&#123;
int FMeterRes;      // position check resolution in meter
int FMaxMeter;      // maximum size in meter of the circle radius

public MPPosInCircle&#40;int Resolution_meter, int MaxRadius_meter&#41;
&#123;
FMeterRes = Resolution_meter;
FMaxMeter = MaxRadius_meter;
&#125;

public delegate void Position&#40;object sender, PositionArgs e&#41;;
public event Position OnPosition;
public class PositionArgs&#58; EventArgs
&#123;
double FLat;
double FLon;
int FRadius;
Boolean FFound;

public PositionArgs&#40;double Lat, double Lon, int Radius&#41;
&#123;
FLat = Lat;
FLon = Lon;
FRadius = Radius;
&#125;

public double Lat
&#123;
get &#123; return FLat; &#125;
&#125;

public double Lon
&#123;
get &#123; return FLon; &#125;
&#125;

public int Meter
&#123;
get &#123; return FRadius; &#125;
&#125;

public Boolean Found
&#123;
get &#123; return FFound; &#125;
set &#123; FFound = value; &#125;
&#125;
&#125;

private double Deg2Rad&#40;double degree&#41;
&#123;
return degree * Math.PI / 180;
&#125;

/*private double Rad2Deg&#40;double rad&#41;
&#123;
return rad * 180 / Math.PI;
&#125;*/

private void CalcXY&#40;double CenterLat, double CenterLon, double Angle, double Radius, out double Lat, out double Lon&#41;
&#123;
/*             B
/|
/ |
/  |
/   |
/    |
c /     | a
/      |
/       |
/        |
/_________|
A     b     C

given A en c
a = c * sin&#40;A&#41;
b = c * cos&#40;A&#41;

A == 0 is direction East in this drawing
a = Latitude offset
b = Longitude offset
c = Radius of circle   */

double r = Deg2Rad&#40;Angle&#41;;
double c = Radius;
double a = c * Math.Sin&#40;r&#41;;
double b = c * Math.Cos&#40;r&#41; / Math.Cos&#40;Deg2Rad&#40;CenterLat&#41;&#41;;  // correct longitude for circle

Lat = CenterLat + a;
Lon = CenterLon + b;
&#125;

public Boolean Execute&#40;double CenterLat, double CenterLon&#41;
&#123;
double Lat, Lon;            // coordinate points on the circumfence of the circle
double Radius;              // radius of the circle in degree
Int32  Circum;              // circumfence of the circle in meter
Int32  Count;               // the amount of points to calculate on the circumfence
Int32  Angle;               // current angle against East to calculate in degree
Int32  AngleInc;            // angle increments in degree per calculation
Int32  Hits  = 0;           // count of hits calculated on the circumfence
Int32  Meter = FMeterRes;   // Radius in meter, 1852 meter is 1 sea mile

while &#40;Meter <= FMaxMeter&#41; &#123;
Radius   = &#40;double&#41;1 / 60 / 1852 * Meter;
Circum   = &#40;Int32&#41;&#40;Meter * 2 * Math.PI&#41;;
Count    = Circum / FMeterRes;
AngleInc = 360 / Count;

if &#40;AngleInc <= 0&#41;
return false;
Angle = 0;
while &#40;Angle < 360&#41; &#123;
CalcXY&#40;CenterLat, CenterLon, Angle, Radius, out Lat, out Lon&#41;;
PositionArgs e = new PositionArgs&#40;Lat, Lon, Meter&#41;;
if&#40;OnPosition != null&#41;
OnPosition&#40;this, e&#41;;
if&#40;e.Found&#41;
Hits++;
Angle += AngleInc;
&#125;
if &#40;Hits > 0&#41;
return true;
Meter += FMeterRes;
&#125;
return false;
&#125;
&#125;```
To use it you can do something like this:

Code:
```        public void PosInCirclePosition&#40;object sender, MPPosInCircle.PositionArgs e&#41;
&#123;
double Alt = 1;
Location Loc;
//Location Loc;
FindResults StreetResults;
Pushpin PP;

Loc = MP.ActiveMap.GetLocation&#40;e.Lat, e.Lon, Alt&#41;;
PP = MP.ActiveMap.AddPushpin&#40;Loc, ""&#41;;
PP.Symbol = 2;
StreetResults = MP.ActiveMap.ObjectsFromPoint&#40;MP.ActiveMap.LocationToX&#40;Loc&#41;, MP.ActiveMap.LocationToY&#40;Loc&#41;&#41;;

foreach&#40;object o in StreetResults&#41; &#123;
Location StreetLoc = o as Location;

if&#40;StreetLoc != null && StreetLoc.StreetAddress != null&#41; &#123;
Console.WriteLine&#40;"Found    " + StreetLoc.StreetAddress.Value&#41;;
//Console.WriteLine&#40;"postcode " + StreetLoc.StreetAddress.PostalCode&#41;;
Console.WriteLine&#40;"Distance " + Convert.ToString&#40;e.Meter&#41; + " meter from point"&#41;;
e.Found = true;
&#125;
&#125;
&#125;```

7. Member Yellow Belt
Join Date
Feb 2005
Posts
32
Hi Wilfried,
Thank you very much!
This will help me a lot, because I didn't understand the math part completely. I'll try the code soon.

Bye,

MD

8. Member Yellow Belt
Join Date
Feb 2005
Posts
32
Hello,
I'm not quite sure, how to use your methods. That's what I tried:
Code:
```				MPPosInCircle.PositionArgs e = new MPPosInCircle.PositionArgs&#40;latitude, longitude, 100&#41;;
double Alt = 1;
Location Loc;
//Location Loc;
FindResults StreetResults;
Pushpin PP;

Loc = map.GetLocation&#40;e.Lat, e.Lon, Alt&#41;;
PP = map.AddPushpin&#40;Loc, ""&#41;;
PP.Symbol = 2;
StreetResults = map.ObjectsFromPoint&#40;map.LocationToX&#40;Loc&#41;, map.LocationToY&#40;Loc&#41;&#41;;

foreach&#40;object o in StreetResults&#41;
&#123;
Location StreetLoc = o as Location;

if&#40;StreetLoc != null && StreetLoc.StreetAddress != null&#41;
&#123;
Console.WriteLine&#40;"Found    " + StreetLoc.StreetAddress.Value&#41;;
//Console.WriteLine&#40;"postcode " + StreetLoc.StreetAddress.PostalCode&#41;;
Console.WriteLine&#40;"Distance " + Convert.ToString&#40;e.Meter&#41; + " meter from point"&#41;;
e.Found = true;
&#125;
&#125;```

I don't know how to create the sender object.

Bye,

MD

9. Senior Member Black Belt
Join Date
Nov 2004
Location
Belgium
Posts
2,433
Hi,

I don't know how to create the sender object.
The code I posted was a quick copy/paste from some code of mine, so it is possible I forgot something However what I did was following:

- find the address
- if not found create a class to search in circles

the class that runs in circles has an event handler and that's where the sender argument comes from.

I'm planning for a while to make an article for this but I dont got on time schedule

so please study the code snippets again and of cource dont hesitate to fire question on things that are not clear !

10. Member Yellow Belt
Join Date
Feb 2005
Posts
32
Hello,
I don't understand when and from where the Execute method is being called.

Bye,

Markus

Page 1 of 2 12 Last

#### Thread Information

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•