问题
Here is the scenario that I need to implement:
Assume that I have a database table that contains Latitudes and Longitudes of all the restaurants of my city, say 500 restaurants. Now I am standing somewhere in the city using my app and want to know my nearby restaurants, say within 10 miles radius.
To achieve this, the app must first know my current location (I have done that). Once my location is known, the app needs to put markers on all the restaurants that are within 10 miles (assume only 25 out of the total 500). I do not need to show the markers for those which are farther than that. Now when I click on any marker I will see the InfoWindow and then clicking on it I need to navigate to a new activity and show some details like address, name, phone number (these all come from database) and its distance from my current location.
Now my two questions:
- I need to show markers only within the given radius and not the rest of those. I too don't want to draw a circle on map. Do I have to traverse all locations and see which lie within the radius or is there a faster and more efficient way?
- Can I have the option of getting both straight-line as well as road distances for all my markers?
回答1:
Write a method in your BI that gives you all the restaurants between the bounds of the Map (or within a given distance from the center (LatLon) of the Map. I used to do it embedding a custom function in Sqlite
[SqliteFunctionAttribute(Name = "distance", Arguments = 4, FuncType = FunctionType.Scalar)]
class SqliteDistance : SqliteFunction
{
public override object Invoke(object[] args)
{
double latA = System.Convert.ToDouble(args[0]);
double lonA = System.Convert.ToDouble(args[1]);
double latB = System.Convert.ToDouble(args[2]);
double lonB = System.Convert.ToDouble(args[3]);
const double R = 6371;
const double pigreco = 3.1415927;
double lat_alfa, lat_beta;
double lon_alfa, lon_beta;
double fi;
double p, d;
/* Converte i gradi in radianti */
lat_alfa = pigreco * latA / 180;
lat_beta = pigreco * latB / 180;
lon_alfa = pigreco * lonA / 180;
lon_beta = pigreco * lonB / 180;
/* Calcola l'angolo compreso fi */
fi = Math.Abs(lon_alfa - lon_beta);
/* Calcola il terzo lato del triangolo sferico */
p = Math.Acos(Math.Sin(lat_beta) * Math.Sin(lat_alfa) +
Math.Cos(lat_beta) * Math.Cos(lat_alfa) * Math.Cos(fi));
/* Calcola la distanza sulla superficie
terrestre R = ~6371 km */
d = p * R;
return (d);
}
}
then in your query :
Public List<Restaurant> GiveMeRestaurants(double fromLat, double fromLon)
{
String sql="Select * from restaurants where distance('" + fromLat.ToString() + "','" + fromLon.ToString() + "',restaurants.Latitude,restaurants.Longitude) <= 100 )";
}
This can give you also the straight line distance.
- For road distance maybe you can call google play services using the center map and the coordinates or the restaurant
回答2:
So you can move this logic in the webservice or do it via LINQ
take a look here Is there a simple way to write a custom function in LINQ to Entities?
来源:https://stackoverflow.com/questions/20827229/show-markers-on-map-within-a-given-radius-xamarin-android