示例#1
0
        public GetGeoXaml()
        {
            InitializeComponent();

            map.MoveToRegion(MapSpan.FromCenterAndRadius(centerPosition, Distance.FromKilometers(4d)));

            button.Clicked += async(sender, e) =>
            {
                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy = 50;

                var location = await locator.GetPositionAsync(10000);

                LatLabel.Text = "Lat: " + location.Latitude.ToString("N6");
                LonLabel.Text = "Lon: " + location.Longitude.ToString("N6");

                var addr = await getAddress.GetJsonAsync(location.Latitude, location.Longitude) ?? "取得できませんでした";

                AddrLabel.Text = "Address: " + addr;

                // Map を移動させてピン打ち
                map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(location.Latitude, location.Longitude), Distance.FromKilometers(4d)));
                if (map.Pins.Count() > 0)
                {
                    map.Pins.Clear();
                }
                pin = new Pin
                {
                    Position = new Position(location.Latitude, location.Longitude),
                    Label    = addr,
                };
                map.Pins.Add(pin);
            };
        }
示例#2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            map.SetRegion(
                new MKCoordinateRegion(
                    centerPosition, // 初期位置
                    new MKCoordinateSpan(0.1d, 0.1d)),
                true);

            locMgr = new CLLocationManager();
            locMgr.RequestWhenInUseAuthorization();

            button.TouchUpInside += (s, _) =>
            {
                locMgr.DesiredAccuracy   = 1000;
                locMgr.LocationsUpdated += async(object sender, CLLocationsUpdatedEventArgs e) =>
                {
                    var location = e.Locations[e.Locations.Length - 1];
                    LatitudeText.Text  = "Lat: " + location.Coordinate.Latitude.ToString("N6");
                    LongitudeText.Text = "Lon: " + location.Coordinate.Longitude.ToString("N6");

                    // PCL で Google Maps API Web サービスに Lat, Lon を投げて住所を取得しています。
                    // Http通信を行う場合、iOSプロジェクトにもSystem.Net.Httpの参照が必要なようです。
                    // https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec
                    var addr = await getAddress.GetJsonAsync(location.Coordinate.Latitude, location.Coordinate.Longitude) ?? "取得できませんでした";

                    System.Diagnostics.Debug.WriteLine("AddressResult", addr);
                    AddrText.Text = "Address: " + addr;

                    // Map 移動
                    map.SetRegion(
                        new MKCoordinateRegion(
                            new CLLocationCoordinate2D(location.Coordinate.Latitude, location.Coordinate.Longitude),
                            new MKCoordinateSpan(0.1d, 0.1d)),
                        true);
                    // Pin 追加
                    map.AddAnnotations(new MKPointAnnotation()
                    {
                        Title      = addr,
                        Coordinate = new CLLocationCoordinate2D(location.Coordinate.Latitude, location.Coordinate.Longitude)
                    });

                    locMgr.StopUpdatingLocation();
                };
                locMgr.StartUpdatingLocation(); // なぜ下に書くのかあめいさんに聞いてみよう。
            };
        }
示例#3
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            map.SetRegion(
                new MKCoordinateRegion(
                    centerPosition, // 初期位置
                    new MKCoordinateSpan(0.1d, 0.1d)),
                true);

            locMgr = new CLLocationManager();
            locMgr.RequestWhenInUseAuthorization();

            button.TouchUpInside += (s, _) =>
            {
                locMgr.DesiredAccuracy   = 1000;
                locMgr.LocationsUpdated += async(object sender, CLLocationsUpdatedEventArgs e) =>
                {
                    var location = e.Locations[e.Locations.Length - 1];
                    LatitudeText.Text  = "Lat: " + location.Coordinate.Latitude.ToString("N6");
                    LongitudeText.Text = "Lon: " + location.Coordinate.Longitude.ToString("N6");

                    // PCL で Google Maps API Web サービスに Lat, Lon を投げて住所を取得しています。
                    var addr = await getAddress.GetJsonAsync(location.Coordinate.Latitude, location.Coordinate.Longitude) ?? "取得できませんでした";

                    System.Diagnostics.Debug.WriteLine("AddressResult", addr);
                    AddrText.Text = "Address: " + addr;

                    // Map 移動
                    map.SetRegion(
                        new MKCoordinateRegion(
                            new CLLocationCoordinate2D(location.Coordinate.Latitude, location.Coordinate.Longitude),
                            new MKCoordinateSpan(0.1d, 0.1d)),
                        true);
                    // Pin 追加
                    map.AddAnnotations(new MKPointAnnotation()
                    {
                        Title      = addr,
                        Coordinate = new CLLocationCoordinate2D(location.Coordinate.Latitude, location.Coordinate.Longitude)
                    });

                    locMgr.StopUpdatingLocation();
                };
                locMgr.StartUpdatingLocation(); // なぜ下に書くのかあめいさんに聞いてみよう。
            };
        }
示例#4
0
        private async void GetLocationButton_Click(object sender, RoutedEventArgs e)
        {
            Geolocator geolocator = new Geolocator();

            geolocator.DesiredAccuracyInMeters = 50;

            try
            {
                Geoposition location = await geolocator.GetGeopositionAsync(
                    maximumAge : TimeSpan.FromMinutes(5),
                    timeout : TimeSpan.FromSeconds(10)
                    );

                Geocoordinate cordinate = location.Coordinate;

                LatitudeText.Text  = "Lat: " + location.Coordinate.Point.Position.Latitude.ToString("N6");
                LongitudeText.Text = "Lon: " + location.Coordinate.Point.Position.Longitude.ToString("N6");

                // PCL で Google Maps API Web サービスに Lat, Lon を投げて住所を取得しています。
                var addr = await getAddress.GetJsonAsync(location.Coordinate.Point.Position.Latitude, location.Coordinate.Point.Position.Longitude) ?? "取得できませんでした";

                System.Diagnostics.Debug.WriteLine("AddressResult", addr);
                AddressText.Text = "Address: " + addr;

                this.map.Center = cordinate.Point;
                map.ZoomLevel   = 15;
            }
            catch (Exception ex)
            {
                if ((uint)ex.HResult == 0x80004004)
                {
                    // the application does not have the right capability or the location master switch is off
                    AddressText.Text = "location  is disabled in phone settings.";
                }
                //else
                {
                    // something else happened acquring the location
                }
            }
        }
示例#5
0
        public async void OnLocationChanged(Android.Locations.Location location)
        {
            marker = null;
            Log.Debug(tag, "Location changed");
            latitude.Text  = "Lat: " + location.Latitude.ToString();
            longitude.Text = "Lon: " + location.Longitude.ToString();
            provider.Text  = "Provider: " + location.Provider.ToString();

            // PCL で Google Geocoding API Web サービスに Lat, Lon を投げて住所を取得しています。
            var addr = await getAddress.GetJsonAsync(location.Latitude, location.Longitude) ?? "取得できませんでした";

            Log.Debug("AddressResult", addr);
            address.Text = "Address: " + addr;

            // 取得した Lat, Lon を Map に投げます。
            map.AnimateCamera(CameraUpdateFactory.NewCameraPosition(
                                  new CameraPosition(
                                      new LatLng(location.Latitude, location.Longitude), 14f, 0f, 0f)));
            marker = map.AddMarker(new MarkerOptions()
                                   .SetTitle(addr)
                                   .SetPosition(new LatLng(location.Latitude, location.Longitude)));
        }
示例#6
0
        public GetGeoCS()
        {
            map = new Map(
                MapSpan.FromCenterAndRadius(
                    centerPosition,              // 初期位置
                    Distance.FromKilometers(2))) // 4km 圏内(?)
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                HasZoomEnabled    = true,
                IsShowingUser     = true,
            };

            LatLabel = new Label
            {
                Text = "Lat:",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                FontSize          = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
            };
            LonLabel = new Label
            {
                Text = "Lon:",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                FontSize          = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
            };
            AddrLabel = new Label
            {
                Text     = "Address:",
                FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
            };
            var getGeoButton = new Button
            {
                Text = "Get Location",
            };

            getGeoButton.Clicked += async(sender, e) =>
            {
                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy         = 50;
                locator.AllowsBackgroundUpdates = false;

                var location = await locator.GetPositionAsync(10000);

                System.Diagnostics.Debug.WriteLine("Lat: " + location.Latitude.ToString("N6") + "Lan: " + location.Longitude.ToString("N6"));
                Device.BeginInvokeOnMainThread(() =>
                {
                    LatLabel.Text = "Lat: " + location.Latitude.ToString("N6");
                    LonLabel.Text = "Lon: " + location.Longitude.ToString("N6");
                });

                var addr = "";
                try
                {
                    addr = await getAddress.GetJsonAsync(location.Latitude, location.Longitude) ?? "取得できませんでした";
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.InnerException);
                }

                AddrLabel.Text = "Address: " + addr;

                // Map を移動させてピン打ち
                map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(location.Latitude, location.Longitude), Distance.FromKilometers(4d)));
                if (map.Pins.Count() > 0)
                {
                    map.Pins.Clear();
                }
                pin = new Pin
                {
                    Position = new Position(location.Latitude, location.Longitude),
                    Label    = addr,
                };
                map.Pins.Add(pin);
            };

            Title   = "XF_GpsSample";
            Content = new StackLayout
            {
                Padding         = new Thickness(8),
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children        =
                {
                    getGeoButton,
                    new StackLayout
                    {
                        Orientation = StackOrientation.Horizontal,
                        Children    =
                        {
                            LatLabel,
                            LonLabel,
                        }
                    },
                    AddrLabel,
                    map
                }
            };
        }