Exemplo n.º 1
0
        public static async Task <string> GetCity()
        {
            var accessStatus = await Geolocator.RequestAccessAsync();

            BasicGeoposition baspos = new BasicGeoposition();

            switch (accessStatus)
            {
            case GeolocationAccessStatus.Allowed:
            {
                Geolocator geolocator = new Geolocator {
                    DesiredAccuracyInMeters = 0
                };
                Geoposition pos = await geolocator.GetGeopositionAsync();

                Geopoint myLocation = pos.Coordinate.Point;
                baspos = myLocation.Position;
                baspos = PositionTransform.Transfrom(baspos);
                const string        MovieSearch_Api = "http://restapi.amap.com/v3/geocode/regeo?key=a3a90de8b27f31e0054c34e61e0e3d9f&location={0}&extensions=base ";
                HttpClient          httpclient      = new HttpClient();
                HttpResponseMessage response        = new HttpResponseMessage();
                string result   = null;
                string Location = Convert.ToString(baspos.Longitude) + "," + Convert.ToString(baspos.Latitude);
                string Api      = MovieSearch_Api.Replace("{0}", Location);
                response = await httpclient.GetAsync(Api);

                result = await response.Content.ReadAsStringAsync();

                return(result);
            }

            case GeolocationAccessStatus.Denied:
            {
                var dialog = new MessageDialog("已拒绝程序对位置的访问权,请至设置-隐私-位置中打开本程序的位置访问权限", "异常提示");
                dialog.Commands.Add(new UICommand("确定", cmd => { }));
                var a = await dialog.ShowAsync();

                bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));

                return("0");
            }

            case GeolocationAccessStatus.Unspecified:
            {
                var dialog = new MessageDialog("未指明程序对位置的访问权,请至设置-隐私-位置中打开本程序的位置访问权限", "异常提示");
                dialog.Commands.Add(new UICommand("确定", cmd => { }));
                var a = await dialog.ShowAsync();

                bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));

                return("1");
            }
            }
            return("2");
        }
        private async Task FirstStep()
        {
            var accessStatus = await Geolocator.RequestAccessAsync();

            BasicGeoposition baspos = new BasicGeoposition();

            switch (accessStatus)
            {
            case GeolocationAccessStatus.Allowed:
            {
                Geolocator geolocator = new Geolocator {
                    DesiredAccuracyInMeters = 0
                };
                Geoposition pos = await geolocator.GetGeopositionAsync();

                myLocation = pos.Coordinate.Point;
                baspos     = myLocation.Position;
                baspos     = PositionTransform.Transfrom(baspos);
                myLocation = new Geopoint(baspos);
                UIElement marker = PointMeMarker();
                MyMap.Children.Add(marker);
                MapControl.SetLocation(marker, myLocation);
                MyMap.Center           = myLocation;
                MyMap.LandmarksVisible = true;
                break;
            }

            case GeolocationAccessStatus.Denied:
            {
                var dialog = new MessageDialog("已拒绝程序对位置的访问权,请至设置-隐私-位置中打开本程序的位置访问权限", "异常提示");
                dialog.Commands.Add(new UICommand("确定", cmd => { }));
                var a = await dialog.ShowAsync();

                bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));

                return;
            }

            case GeolocationAccessStatus.Unspecified:
            {
                var dialog = new MessageDialog("未指明程序对位置的访问权,请至设置-隐私-位置中打开本程序的位置访问权限", "异常提示");
                dialog.Commands.Add(new UICommand("确定", cmd => { }));
                var a = await dialog.ShowAsync();

                bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));

                return;
            }
            }
            string position = baspos.Longitude + "," + baspos.Latitude;
            string json     = null;

            try
            {
                json = await Httprequest.HttpRequest.GetPositionSearchRequest(position, SerchRange);
            }
            catch (HttpRequestException)
            {
                var dialog = new MessageDialog("周边检索异常(未能成功发送请求)", "异常提示");
                dialog.Commands.Add(new UICommand("确定", cmd => { }));
                var a = await dialog.ShowAsync();
            }
            if (!string.IsNullOrWhiteSpace(json))
            {
                POIResult           = JsonConvert.DeserializeObject <Rootobject1>(json);
                viewmodel.poiresult = POIResult.pois;
                foreach (Pois i in POIResult.pois)
                {
                    Geopoint geo      = LocationToGeoPoint(i.location);
                    MapIcon  mapIcon1 = new MapIcon();
                    mapIcon1.Location = geo;
                    mapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0);
                    mapIcon1.Title = i.name;
                    MyMap.MapElements.Add(mapIcon1);
                }
            }
        }