如何在Xamarin应用程序的地图功能中获取当前位置坐标? [英] How to get current location coordinates in the Map feature in Xamarin Application?

查看:227
本文介绍了如何在Xamarin应用程序的地图功能中获取当前位置坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用该应用程序中的位置坐标标记功能.

I am working on location coordinates marking feature in the App.

我正在使用以下dll和google地图.

I am using following dll's and google map.

Xamarin.Forms.Maps
Xam.Plugin.Geolocator
Xam.Plugin.ExternalMaps

在我的iPhone模拟器中,如果位置"为无",它将默认位置拉到意大利罗马的某个地方

In my iphone simulator, If the Location is NONE.It pulls default location some where in Rome, Italy

以下是为拉出地图而编写的代码.

Following is the code written to pull the Map ..

public async Task<Xamarin.Forms.Maps.Position> GetPosition()
        {
            IsBusy = true;
            Xamarin.Forms.Maps.Position p;
            try
            {
                if (!locator.IsGeolocationAvailable)
                {
                    p = new Xamarin.Forms.Maps.Position();
                }
                if (!locator.IsGeolocationEnabled)
                {
                    p = new Xamarin.Forms.Maps.Position();
                }
                var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
                p = new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                p = new Xamarin.Forms.Maps.Position();
            }
            IsBusy = false;

            return p;
        }

        public async Task<object> GetCityName(double latitude, double longitude) { 
            HttpClient client;
            client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
            try
            {
                var response = await client.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude);
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    var json = Newtonsoft.Json.Linq.JObject.Parse(result);
                    var reValue = "" + json["results"][0]["formatted_address"];

                    var strArr = reValue.Split(',');
                    if (strArr.Length > 2)
                        return strArr[strArr.Length - 2] + ", " +strArr[strArr.Length - 1];
                    else
                        return "";
                }
                else {
                    Debug.WriteLine(@"Failed.");
                    return "Failed";
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"ERROR {0}", ex.Message);
                return ex.Message;
            }
        }

图片:

推荐答案

请详细说明您的问题,并按以下步骤更改条件:

elaborate your problem please, and change your conditions as follows:

if (CrossGeolocator.Current.IsGeolocationAvailable)
{
   if (CrossGeolocator.Current.IsGeolocationEnabled)
   {
      var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
      p = new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude);
   }
   else
   {
      throw new Exception("Geolocation is turned off");
      // Geolocation is turned off for the device.
   }
}
else
{ 
    throw new Exception("Geolocation is turned off");
    // Geolocation not available for device
}

现在在这里处理GetPosition()方法中的渔获物,以确保您未获取默认位置.

Here now Handle the catches from GetPosition() Method to make sure you don't get the default location.

这篇关于如何在Xamarin应用程序的地图功能中获取当前位置坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆