Reputation: 21
I'm looking to create a function that will allow me to search for an address using the API. Then from that address want to be able to set a 50 mile radius of that location, and be able to see the other addresses in our database that are in the 50 mile radius. The 50 mile radius can change, but that's just for example.
Upvotes: 2
Views: 4886
Reputation: 6791
You can try Places API
The Google Places API Web Service allows you to query for place information on a variety of categories, such as: establishments, prominent points of interest, geographic locations, and more. You can search for places either by proximity or a text string. A Place Search returns a list of places along with summary information about each place; additional information is available via a Place Details query.
As suggested in this related SO post, Places API response is in JSON format. The community recommend to us json2csharp to easily generate a C# model for the response to a Google Places query. Then use JSON.NET to deserialize the query result.
Here is a sample code query:
using (var client = new HttpClient())
{
var response = await client.GetStringAsync(string.Format("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={0},{1}&radius=500&type=bar&key=YourAPIKey", latitude, longitude));
var result = JsonConvert.DeserializeObject<PlacesApiQueryResponse>(response);
}
Then follow the code implementation on how to check the distance of the search address and other addresses in the database. They used computeDistanceBetween
that returns the distance, in meters, between two LatLngs.
var marker_lat_lng = new google.maps.LatLng(location.lat, location.lng);
var distance_from_location = google.maps.geometry.spherical.computeDistanceBetween(address_lat_lng, marker_lat_lng); //distance in meters between your location and the marker
if (distance_from_location <= radius_km * 1000) {
var new_marker = new google.maps.Marker({
position: marker_lat_lng,
map: map,
title: location.name
});
Upvotes: 0
Reputation: 1567
This should help you. You can get the longitude and latitude from the Google maps API or elsewhere if it suits you better. Then use this method to calculate the distance.
http://www.geodatasource.com/developers/c-sharp
Upvotes: 1