Community of VE/MapPoint Users and Developers
This is a discussion on Getting street from latitude/longitude? within the MapPoint 2006/2009 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. ...
| |||||||
| Register | Blogs | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| Getting street from latitude/longitude? 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 |
| |||
|
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(Lat, Lon, 1);
FindResults StreetResults = map.ObjectsFromPoint(map.LocationToX(Loc), map.LocationToY(Loc));
foreach (object o in StreetResults) {
Location StreetLoc = o as Location;
if(StreetLoc != null && StreetLoc.StreetAddress != null) {
Console.WriteLine("value " + StreetLoc.StreetAddress.Value);
Console.WriteLine("street " + StreetLoc.StreetAddress.Street);
Console.WriteLine("postcode " + StreetLoc.StreetAddress.PostalCode);
Console.WriteLine("city " + StreetLoc.StreetAddress.City);
}
}
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
| |||
|
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 |
| |||
|
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(latitude, longitude, 1);
Loc.GoTo();
FindResults StreetResults = map.ObjectsFromPoint(map.LocationToX(Loc), map.LocationToY(Loc));
Boolean foundStreet = false;
foreach (object o in StreetResults)
{
if (o is Location)
{
Location StreetLoc = o as Location;
if(StreetLoc != null && StreetLoc.StreetAddress != null)
{
text += "\"" + dateTime + "\",\"" + StreetLoc.StreetAddress.PostalCode + "\",\"" + StreetLoc.StreetAddress.City + "\",\"" + StreetLoc.StreetAddress.Street + "\",\"" + latitude + "\",\"" + longitude + "\"\r\n";
foundStreet = true;
break;
}
}
}
// no street found ==> search around in circles
if (!foundStreet)
{
double maxDistance = 0.4;
double distance = 0.05;
while (distance <= maxDistance)
{
double radius = 1 / 60 / 1852 * distance;
double circum = Math.Floor(distance * 2 * Math.PI);
double count = circum / 5;
double angleInc = 360 / count;
if (angleInc <= 0)
{
foundStreet = false;
break;
}
double angle = 0;
while (angle < 360)
{
double lat = calcLat(latitude, longitude, angle, radius);
double lon = calcLon(latitude, longitude, angle, radius);
Loc = map.GetLocation(lat, lon, 1);
Loc.GoTo();
StreetResults = map.ObjectsFromPoint(map.LocationToX(Loc), map.LocationToY(Loc));
// Search with new coordinates
foreach (object o in StreetResults)
{
if (o is Location)
{
Location StreetLoc = o as Location;
if(StreetLoc != null && StreetLoc.StreetAddress != null)
{
text += "\"" + dateTime + "\",\"" + StreetLoc.StreetAddress.PostalCode + "\",\"" + StreetLoc.StreetAddress.City + "\",\"" + StreetLoc.StreetAddress.Street + "\",\"" + latitude + "\",\"" + longitude + "\"\r\n";
foundStreet = true;
break;
}
}
}
if (foundStreet)
break;
angle = angle + angleInc;
}
if (foundStreet)
{
break;
}
distance = distance + 0.05;
}
}
if (!foundStreet)
{
text += "\"" + dateTime + "\",\"---\",\"---\",\"---\",\"" + latitude + "\",\"" + longitude + "\"\r\n";
}
}
private static double calcLon (double latitude, double longitude, double angle, double radius)
{
double r = angle * Math.PI / 180;
double c = radius;
double b = c * Math.Cos(r) / Math.Cos((latitude * Math.PI / 180));
return longitude + b;
}
private static double calcLat (double latitude, double longitude, double angle, double radius)
{
double r = angle * Math.PI / 180;
double c = radius;
double a = c * Math.Sin(r);
return latitude + a;
}
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 |
| |||
|
Hi, I made the class in C3 for you. this is the code: Code: public class MPPosInCircle
{
int FMeterRes; // position check resolution in meter
int FMaxMeter; // maximum size in meter of the circle radius
public MPPosInCircle(int Resolution_meter, int MaxRadius_meter)
{
FMeterRes = Resolution_meter;
FMaxMeter = MaxRadius_meter;
}
public delegate void Position(object sender, PositionArgs e);
public event Position OnPosition;
public class PositionArgs: EventArgs
{
double FLat;
double FLon;
int FRadius;
Boolean FFound;
public PositionArgs(double Lat, double Lon, int Radius)
{
FLat = Lat;
FLon = Lon;
FRadius = Radius;
}
public double Lat
{
get { return FLat; }
}
public double Lon
{
get { return FLon; }
}
public int Meter
{
get { return FRadius; }
}
public Boolean Found
{
get { return FFound; }
set { FFound = value; }
}
}
private double Deg2Rad(double degree)
{
return degree * Math.PI / 180;
}
/*private double Rad2Deg(double rad)
{
return rad * 180 / Math.PI;
}*/
private void CalcXY(double CenterLat, double CenterLon, double Angle, double Radius, out double Lat, out double Lon)
{
/* B
/|
/ |
/ |
/ |
/ |
c / | a
/ |
/ |
/ |
/_________|
A b C
given A en c
a = c * sin(A)
b = c * cos(A)
A == 0 is direction East in this drawing
a = Latitude offset
b = Longitude offset
c = Radius of circle */
double r = Deg2Rad(Angle);
double c = Radius;
double a = c * Math.Sin(r);
double b = c * Math.Cos(r) / Math.Cos(Deg2Rad(CenterLat)); // correct longitude for circle
Lat = CenterLat + a;
Lon = CenterLon + b;
}
public Boolean Execute(double CenterLat, double CenterLon)
{
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 (Meter <= FMaxMeter) {
Radius = (double)1 / 60 / 1852 * Meter;
Circum = (Int32)(Meter * 2 * Math.PI);
Count = Circum / FMeterRes;
AngleInc = 360 / Count;
if (AngleInc <= 0)
return false;
Angle = 0;
while (Angle < 360) {
CalcXY(CenterLat, CenterLon, Angle, Radius, out Lat, out Lon);
PositionArgs e = new PositionArgs(Lat, Lon, Meter);
if(OnPosition != null)
OnPosition(this, e);
if(e.Found)
Hits++;
Angle += AngleInc;
}
if (Hits > 0)
return true;
Meter += FMeterRes;
}
return false;
}
}
Code: public void PosInCirclePosition(object sender, MPPosInCircle.PositionArgs e)
{
double Alt = 1;
Location Loc;
//Location Loc;
FindResults StreetResults;
Pushpin PP;
Loc = MP.ActiveMap.GetLocation(e.Lat, e.Lon, Alt);
PP = MP.ActiveMap.AddPushpin(Loc, "");
PP.Symbol = 2;
StreetResults = MP.ActiveMap.ObjectsFromPoint(MP.ActiveMap.LocationToX(Loc), MP.ActiveMap.LocationToY(Loc));
foreach(object o in StreetResults) {
Location StreetLoc = o as Location;
if(StreetLoc != null && StreetLoc.StreetAddress != null) {
Console.WriteLine("Found " + StreetLoc.StreetAddress.Value);
//Console.WriteLine("postcode " + StreetLoc.StreetAddress.PostalCode);
Console.WriteLine("Distance " + Convert.ToString(e.Meter) + " meter from point");
e.Found = true;
}
}
}
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
| |||
|
Hello, I'm not quite sure, how to use your methods. That's what I tried: Code: MPPosInCircle.PositionArgs e = new MPPosInCircle.PositionArgs(latitude, longitude, 100);
double Alt = 1;
Location Loc;
//Location Loc;
FindResults StreetResults;
Pushpin PP;
Loc = map.GetLocation(e.Lat, e.Lon, Alt);
PP = map.AddPushpin(Loc, "");
PP.Symbol = 2;
StreetResults = map.ObjectsFromPoint(map.LocationToX(Loc), map.LocationToY(Loc));
foreach(object o in StreetResults)
{
Location StreetLoc = o as Location;
if(StreetLoc != null && StreetLoc.StreetAddress != null)
{
Console.WriteLine("Found " + StreetLoc.StreetAddress.Value);
//Console.WriteLine("postcode " + StreetLoc.StreetAddress.PostalCode);
Console.WriteLine("Distance " + Convert.ToString(e.Meter) + " meter from point");
e.Found = true;
}
}
I don't know how to create the sender object. Bye, MD |
| |||
|
Hi, Quote:
- 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 !
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to get latitude and longitude | ma2005pp | MapPoint 2006/2009 Discussion | 3 | 09-28-2005 01:57 AM |
| Latitude and Longitude | virgilar | MapPoint 2006/2009 Discussion | 1 | 03-21-2005 01:01 PM |
| Latitude and Longitude | Anonymous | MapPoint 2006/2009 Discussion | 1 | 11-25-2003 12:52 PM |
| NMEA latitude/longitude and mappoint latitude/longitude | muurman | MapPoint 2006/2009 Discussion | 3 | 11-22-2003 04:42 AM |
| VB6 - Latitude and longitude of a pushpin ??? | Vincent BENNER | MapPoint 2006/2009 Discussion | 1 | 11-12-2002 06:03 AM |
Inclusive Ski Holidays
For inclusive ski holidays to suit your budget, visit Holiday Hypermarket online today. Some stunning ski slopes are just a few clicks away.
Late Deal Holidays
Whatever your travel needs, city breaks, late deal holidays, luxury holidays or family holidays - your personal Travel Counsellor can help
Holidays to Thailand
The best cultures and backgrounds make Thailand an interesting and memorable country to visit. Book great value holidays to Thailand online at dealchecker.co.uk.
Holidays in Dubai
Holidays in Dubai are an eclectic mix of the ancient and the modern. Discover an oasis of luxury amid the Arabian desert. Book here now!
Cheap Morocco Holidays
Cheap Morocco holidays may be the answer to your cheap holiday search. With sunshine throughout most of the year it can be great value if you avoid the peak season. Why not include a trip to the small tranquil town of Chefchaouen Tangier in your visit?
Holiday Comparison
We can help you with holiday comparison when you check out the options at Travel.co.uk
Holidays in Gran Canaria
Have some fun in the sun on the Canary Islands! See On The Beach for information on holidays in Gran Canaria.