Um die Standorte in der Nähe zu finden, kann man die Google Places API Web Service
in Xamarin forms
verwenden. Mit dieser api
können die Google Places-Daten von Google-Servern abgerufen werden. Die Orte Daten enthält Informationen über Restaurants, Lodges, Krankenhäuser, Gesundheitszentren und vieles mehr. Die Probe ist dies:
public async Task<List<Place>> GetNearByPlacesAsync()
{
RootObject rootObject=null;
client = new HttpClient();
CultureInfo In = new CultureInfo("en-IN");
string latitude = position.Latitude.ToString(In);
string longitude = position.Longitude.ToString(In);
string restUrl = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+latitude+","+longitude+"&radius=1000&type="+search+"&key=your API";
var uri = new Uri(restUrl);
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
rootObject = JsonConvert.DeserializeObject<RootObject>(content);
}
else
{
await Application.Current.MainPage.DisplayAlert("No web response", "Unable to retrieve information, please try again", "OK");
}
return rootObject.results;
}
Dieses Stammobjekt haben alle Informationen in der Web-Anfrage gefragt. Jetzt können Sie den Standort in der Nähe über pins
wie diese zeigen:
private async void AddPins()
{
try
{
var Places = await GetNearByPlacesAsync();
if (Places.Count == 0|| Places==null)
await Application.Current.MainPage.DisplayAlert("No Places found", "No Places found for this location", "OK");
else
{
map.Pins.Clear();
foreach(Place place in Places)
{
map.Pins.Add(new Pin()
{
BindingContext = place,
Tag = place,
Label = place.name,
Position = new Position(place.geometry.location.lat, place.geometry.location.lng),
});
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(@" ERROR{0}", ex.Message);
}
}
Die map
ist der Name Map Element
in UI.