부근의 위치를 찾으려면 Google Places API Web Service
을 Xamarin forms
에 입력하십시오. 이 api
을 사용하면 Google 장소 데이터가 Google 서버에서 검색 될 수 있습니다. 장소 데이터에는 레스토랑, 롯지, 병원, 보건 센터 등의 정보가 포함됩니다. 이 샘플은 다음과 같습니다.
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;
}
이 루트 개체는 웹 요청에서 요청 된 모든 정보를 갖습니다. 지금 당신은이 같은 pins
를 통해 근처의 위치를 표시 할 수 있습니다
map
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);
}
}
은 UI에서 Map Element
의 이름입니다.