Xamarin-靠近位置

时间:2017-03-06 00:15:34

标签: xamarin xamarin.ios xamarin.android

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

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

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

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

但我怎样才能到达附近? 我想用Xamarin Forms。

2 个答案:

答案 0 :(得分:1)

  

但我怎样才能到达附近?我想用Xamarin Forms。

我猜您按照官方文档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来找到一个地方,我们需要在每个平台上实现这个功能。

答案 1 :(得分:0)

要查找附近的位置,可以使用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;
    }

此根对象具有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是用户界面中Map Element的名称。