【问题标题】:Xamarin-Get near by locationXamarin-按位置获取附近
【发布时间】:2017-12-27 14:27:26
【问题描述】:

我找到了获取该位置的纬度和经度的代码。现在,我想找一个附近的位置。比如在附近找一家餐馆。

所以,如果我搜索地铁,我应该得到附近地铁的坐标。

要查找我使用此代码的位置:

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

但是我怎样才能得到附近的位置? 我想要 Xamarin 表单。

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.android


    【解决方案1】:

    但是我怎样才能得到附近的位置?我想要 Xamarin 表单。

    我猜你是按照官方文档Adding a Map in Xamarin.Forms 来构建你的应用程序,但是 Xamarin Forms 的APIs for Map 仅用于显示和注释地图。

    对于Android平台,指南中使用的地图服务是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找附近的地方。另外,我们需要使用DependencyService从PCL调用iOS平台API。

    无论如何,目前还没有简单的方法可以从 PCL 调用 API 来查找位置,我们需要在每个平台上实现此功能。

    【讨论】:

      【解决方案2】:

      要查找附近的位置,可以使用Xamarin forms 中的Google Places API Web Service。使用这个api 可以从谷歌服务器检索谷歌地方数据。地点数据包括有关餐馆、旅馆、医院、保健中心等的信息。示例是这样的:

       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;
          }
      

      此根对象包含 Web 请求中询问的所有信息。现在您可以通过pins 显示附近的位置,如下所示:

      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);
              }
          }
      

      map 是 UI 中 Map Element 的名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-11
        相关资源
        最近更新 更多