2017-03-06 1 views
0

위도와 경도를 얻는 코드를 찾았습니다. 이제, 가까운 위치를 찾고 싶습니다. 근처 레스토랑 찾기.Xamarin- 가까운 위치로 이동

지하철을 검색하면 지하철로 가까운 지역의 좌표를 받아야합니다.

이 코드 사용 위치 확인하는 방법은 다음과 같습니다

var locator = CrossGeolocator.Current; 
      locator.DesiredAccuracy = 50; 
      var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); 

을하지만 어떻게 위치하여 근처를받을 수 있나요? 나는 Xamarin Forms에서 원한다.

답변

1

하지만 어떻게 가까이에 위치 할 수 있습니까? 나는 Xamarin Forms에서 원한다.

나는 당신이 당신의 응용 프로그램을 구축 할 수있는 공식 문서 Adding a Map in Xamarin.Forms을 따라 생각하지만, 자 마린 양식의 APIs for Map에만 표시 및 주석 달기지도입니다.

안드로이드 플랫폼의 경우 가이드에 사용 된지도 서비스는 Google Map이며 가까운 위치를 찾으려면 Places API for Android을 사용해야하며이 작업을 수행 할 Xamarin Forms API가 없습니다. Xamarin.Forms는 개발자가 플랫폼간에 공유되는 고유 한 사용자 인터페이스 레이아웃을 만들 수있는 크로스 플랫폼 UI 툴킷이므로 DependencyService을 사용하여 PCL의 플랫폼 관련 기능을 호출해야합니다.

iOS 플랫폼의 경우 iOS 개발에 익숙하지 않지만 Xamarin Forms 툴킷의지도는 기본 iOS지도를 사용한다고 생각합니다. iOS 기본지도의지도 서비스는 사용자가있는 지역에 따라 다릅니다. 아르. 하지만 iOS 공식 문서 About Location Services and Maps을 따라 근처 장소를 찾을 수 있습니다. 또한 PCL에서 iOS 플랫폼 API를 호출하려면 DependencyService을 사용해야합니다.

어쨌든 PCL에서 API를 호출하여 현재 장소를 찾는 쉬운 방법은 없지만 각 플랫폼에서이 기능을 구현해야합니다.

0

부근의 위치를 ​​찾으려면 Google Places API Web ServiceXamarin 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의 이름입니다.