async void OnGetDetails(object sender, GoogleMap.MapClickEventArgs e)
        {
            /* The Geocoder class is used to issue address queries to Google's servers,
             * and to receive a specific Address object */
            Geocoder geocoder = new Geocoder(this);

            // Use the GetFromLocationAsync method to return a single result for the passed point in the MapClickedEventArgs.
            var results = await geocoder.GetFromLocationAsync(e.Point.Latitude, e.Point.Longitude, 1);

            /* If the method returns a value (check the returning array's count), then
             * take the resulting Address object and create a new Marker to display it on the map. */
            if (results.Count > 0)
            {
                var result = results[0];

                if (lastGeoMarker == null)
                {
                    var markerOptions = new MarkerOptions()
                                        .SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueOrange))
                                        .SetPosition(new LatLng(result.Latitude, result.Longitude))
                                        .SetTitle(result.FeatureName)
                                        .SetSnippet(GetAddress(result));

                    lastGeoMarker = map.AddMarker(markerOptions);
                }
                else
                {
                    lastGeoMarker.Position = new LatLng(result.Latitude, result.Longitude);
                    lastGeoMarker.Title    = result.FeatureName;
                    lastGeoMarker.Snippet  = GetAddress(result);
                }

                lastGeoMarker.ShowInfoWindow();
            }
        }
示例#2
0
        public static async Task <MapLocationFinderResult> FindLocationsAtAsync(CancellationToken ct, Geopoint queryPoint)
        {
            if (!Geocoder.IsPresent)
            {
                return(new MapLocationFinderResult(
                           locations: new List <MapLocation>().AsReadOnly(),
                           status: MapLocationFinderStatus.UnknownError
                           ));
            }

            var addresses = await _geocoder.GetFromLocationAsync(
                queryPoint.Position.Latitude,
                queryPoint.Position.Longitude,
                maxResults : MaxResults
                );

            var locations = addresses.Select(loc =>
            {
                var point = new Geopoint(new BasicGeoposition
                {
                    Latitude  = loc.Latitude,
                    Longitude = loc.Longitude
                });

                // Known differences:
                // ==================
                // - Android seems to provide 2 types of address:
                //      - Precise addresses with street, streetNumber and postCode (no district)
                //      - Less precise addresses with district (no street, streetNumber or postCode)
                // - Precise addresses usually come first in the list
                // - It might be possible to merge a precise address with a less precise one in order to get the district information (which Windows does provide)
                // - Other known differences are listed below
                var address = new MapAddress(
                    buildingFloor: string.Empty,                                                                                  // not supported
                    buildingName: string.Empty,                                                                                   // not supported
                    buildingRoom: string.Empty,                                                                                   // not supported
                    buildingWing: string.Empty,                                                                                   // not supported
                    formattedAddress: string.Join(", ", Enumerable.Range(0, loc.MaxAddressLineIndex).Select(loc.GetAddressLine)), // differs from Windows
                    continent: string.Empty,                                                                                      // not supported
                    country: loc.CountryName,
                    countryCode: loc.CountryCode,                                                                                 // differs from Windows (i.e., "CA" instead of "CAN")
                    district: loc.SubLocality,                                                                                    // seems to be null if a specific address is found (streetNumber, street and postCode)
                    neighborhood: loc.SubLocality,                                                                                // haven't seen a non-null value yet (on both Windows and Android)
                    postCode: loc.PostalCode,
                    region: loc.AdminArea,
                    regionCode: string.Empty,          // haven't seen a non-null value yet (on both Windows and Android)
                    street: loc.Thoroughfare,
                    streetNumber: loc.SubThoroughfare, // usually is a range (i.e., "706-212" instead of "706")
                    town: loc.Locality
                    );

                return(new MapLocation(point, address));
            })
                            .ToList()
                            .AsReadOnly();

            var status = MapLocationFinderStatus.Success; // TODO

            return(new MapLocationFinderResult(locations, status));
        }
示例#3
0
        public async Task <string> GetCityFromLocation(GeoLocation location)
        {
            var geocoder      = new Geocoder(Application.Context);
            var foundLocation = await geocoder.GetFromLocationAsync(location.Latitude, location.Longitude, 1);

            return(foundLocation.FirstOrDefault().Locality);
        }
示例#4
0
        public async Task <Address> GetAddressForPositionAsync(double latitude, double longitude)
        {
            Geocoder geocoder = new Geocoder(Forms.Context, Locale.English);

            var addresses = await geocoder.GetFromLocationAsync(latitude, longitude, 1);

            var address = addresses.FirstOrDefault();

            if (address == null)
            {
                return(new Address
                {
                    City = "Schriesheim",
                    PostalCode = "69198",
                    CountryCode = "DE",
                    CountryName = "Germany"
                });
            }
            else
            {
                return(new Address
                {
                    City = address.Locality,
                    PostalCode = address.PostalCode,
                    CountryCode = address.CountryCode,
                    CountryName = address.CountryName
                });
            }

            //return null;
        }
        //http://blog.csdn.net/litton_van/article/details/7101422
        //http://blog.csdn.net/i_lovefish/article/details/7948215

        public async Task <string> GetCityNameAsync()
        {
            var lm = LocationManager.FromContext(Forms.Context);

            Criteria criteria = new Criteria();

            criteria.Accuracy         = Accuracy.Low; //高精度
            criteria.AltitudeRequired = false;        //不要求海拔
            criteria.BearingRequired  = false;        //不要求方位
            criteria.CostAllowed      = false;        //不允许有话费
            criteria.PowerRequirement = Power.Low;    //低功耗

            var provider = lm.GetBestProvider(criteria, true);

            var loc = lm.GetLastKnownLocation(provider);//LocationManager.GpsProvider

            if (loc != null)
            {
                using (var g = new Geocoder(Forms.Context)) {
                    var addrs = await g.GetFromLocationAsync(loc.Latitude, loc.Longitude, 1);

                    if (addrs != null && addrs.Count > 0)
                    {
                        var addr = addrs.First();
                        return(addr.Locality);
                    }
                }
            }
            return("深圳");
        }
示例#6
0
        async void OnGetDetails(object sender, GoogleMap.MapClickEventArgs e)
        {
            var geocoder = new Geocoder(this);
            var results  = await geocoder.GetFromLocationAsync(e.Point.Latitude, e.Point.Longitude, 1);

            if (results.Count > 0)
            {
                var result = results[0];

                if (lastGeoMarker == null)
                {
                    var markerOptions = new MarkerOptions()
                                        .SetIcon(BitmapDescriptorFactory.DefaultMarker(
                                                     BitmapDescriptorFactory.HueOrange))
                                        .SetPosition(new LatLng(result.Latitude, result.Longitude))
                                        .SetTitle(result.FeatureName)
                                        .SetSnippet(GetAddress(result));
                    lastGeoMarker = map.AddMarker(markerOptions);
                }
                else
                {
                    lastGeoMarker.Position = new LatLng(result.Latitude, result.Longitude);
                    lastGeoMarker.Title    = result.FeatureName;
                    lastGeoMarker.Snippet  = GetAddress(result);
                }

                lastGeoMarker.ShowInfoWindow();
            }
        }
        /// <summary>
        /// Gets the address from location async. This may throw a timeout exception if the geocoder fails to work.
        /// </summary>
        /// <returns>The address from location async.</returns>
        /// <param name="location">Location.</param>
        public async Task <ION.Core.Location.Address> GetAddressFromLocationAsync(ILocation location)
        {
            var lat       = location.latitude;
            var lng       = location.longitude;
            var geocoder  = new Geocoder(ion.context, Java.Util.Locale.Default);
            var addresses = await geocoder.GetFromLocationAsync(lat, lng, 1);

            if (addresses == null || addresses.Count <= 0)
            {
                return(new ION.Core.Location.Address());
            }
            var first = addresses[0];

            if (first == null)
            {
                return(new ION.Core.Location.Address());
            }
            else
            {
                return(new ION.Core.Location.Address()
                {
                    address1 = first.GetAddressLine(0),
                    city = first.Locality,
                    state = first.AdminArea,
                    zip = first.PostalCode,
                });
            }
        }
        private async Task <Address> ReverseGeocodeCurrentLocation(LatLng latLng)
        {
            try
            {
                var      locale  = Resources.Configuration.Locale;
                Geocoder geocode = new Geocoder(this, locale);

                var addresses = await geocode.GetFromLocationAsync(latLng.Latitude, latLng.Longitude, 2); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                if (addresses.Count > 0)
                {
                    //string address = addresses[0].GetAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                    //string city = addresses[0].Locality;
                    //string state = addresses[0].AdminArea;
                    //string country = addresses[0].CountryName;
                    //string postalCode = addresses[0].PostalCode;
                    //string knownName = addresses[0].FeatureName; // Only if available else return NULL
                }
                else
                {
                    //Error Message
                    Toast.MakeText(this, GetText(Resource.String.Lbl_Error_DisplayAddress), ToastLength.Short).Show();
                }

                return(addresses.FirstOrDefault());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(null);
            }
        }
示例#9
0
        async void AddressButton_OnClick(object sender, EventArgs e)
        {
            if (_currentLocation == null)
            {
                _addressText.Text = "Can't determine current address.";
                return;
            }

            Geocoder        geocoder    = new Geocoder(this);
            IList <Address> addressList = await geocoder.GetFromLocationAsync
                                              (_currentLocation.Latitude, _currentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            if (address != null)
            {
                StringBuilder deviceAddress = new StringBuilder();
                for (int i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceAddress.Append(address.GetAddressLine(i)).AppendLine(",");
                }
                _addressText.Text = deviceAddress.ToString();
            }
            else
            {
                _addressText.Text = "Unable to determine the address.";
            }
        }
示例#10
0
        private async Task <Address> ReverseGeocodeCurrentLocation(LatLng latLng)
        {
            try
            {
                #pragma warning disable 618
                var locale = (int)Build.VERSION.SdkInt < 25 ? Resources?.Configuration?.Locale : Resources?.Configuration?.Locales?.Get(0) ?? Resources?.Configuration?.Locale;
                #pragma warning restore 618
                Geocoder geocode = new Geocoder(this, locale);

                var addresses = await geocode.GetFromLocationAsync(latLng.Latitude, latLng.Longitude, 2); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                if (addresses.Count > 0)
                {
                    //string address = addresses[0].GetAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                    //string city = addresses[0].Locality;
                    //string state = addresses[0].AdminArea;
                    //string country = addresses[0].CountryName;
                    //string postalCode = addresses[0].PostalCode;
                    //string knownName = addresses[0].FeatureName; // Only if available else return NULL
                }
                else
                {
                    //Error Message
                    Toast.MakeText(this, GetText(Resource.String.Lbl_Error_DisplayAddress), ToastLength.Short)?.Show();
                }

                return(addresses.FirstOrDefault());
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
                return(null !);
            }
        }
示例#11
0
        async public void OnLocationChanged(Location location)
        {
            currentLocation = location;
            try
            {
                new Thread(new ParameterizedThreadStart(_BlindRequestAfterSessionRefresh)).Start(string.Format(
                                                                                                     "http://system.servssl.org.uk/Service/MobileService.asmx/UpdateLocation?lat={0}&lng={1}",
                                                                                                     location.Latitude, location.Longitude));
            }
            catch { }
            try
            {
                Geocoder        geocoder    = new Geocoder(this);
                IList <Address> addressList = await geocoder.GetFromLocationAsync(currentLocation.Latitude, currentLocation.Longitude, 10);

                Address address = addressList.FirstOrDefault();
                if (address != null)
                {
                    lblLocation.Text = string.Format("{0}", address.SubLocality);
                }
                else
                {
                    lblLocation.Text = "Unable to determine the address.";
                }
            }
            catch
            {
                lblLocation.Text = "Check connection!";
            }
        }
示例#12
0
        public async void UpdateLocation()
        {
            if (_currentLocation == null)
            {
                _addressText = "Can't determine the current address.";
                return;
            }

            Geocoder        geocoder    = new Geocoder(this);
            IList <Address> addressList = await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            if (address != null)
            {
                string deviceAddress = "";
                for (int i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceAddress += (address.GetAddressLine(i)) + ", ";
                }
                _addressText = deviceAddress;
            }
            else
            {
                _addressText = "Unable to determine the address.";
            }
        }
示例#13
0
        async void OutputAddress()
        {
            if (_currentLocation == null)
            {
                _addresstext.Text = "Failed to get location";
            }

            Geocoder geocoder = new Geocoder(this);
            //Look up collection of Address objects for current location. max results of 10
            IList <Address> addressList = await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

            //Select the first address if possible
            Address currentAddress = addressList.FirstOrDefault();

            if (currentAddress == null)
            {
                //RIP
                _addresstext.Text = "Failed to determine address";
            }
            else
            {
                //Build string to be printed to the screen
                StringBuilder deviceAddress = new StringBuilder();
                for (int i = 0; i < currentAddress.MaxAddressLineIndex; i++)
                {
                    deviceAddress.AppendLine(currentAddress.GetAddressLine(i));
                }
                _addresstext.Text = deviceAddress.ToString();
            }
        }
示例#14
0
        public async Task <LocationAddress> ReverseGeoCodeLatLonAsync(double lat, double lon)
        {
            var geo       = new Geocoder(Forms.Context);
            var ispresent = Geocoder.IsPresent;

            IList <Address> addresses = null;

            do
            {
                addresses = await geo.GetFromLocationAsync(lat, lon, 1);
            } while (!addresses.Any());

            if (addresses.Any())
            {
                var place = new LocationAddress();
                var add   = addresses[0];
                place.Name = add.FeatureName;
                if (add.MaxAddressLineIndex > 0)
                {
                    place.Address1 = add.GetAddressLine(0);
                }
                place.City     = add.Locality;
                place.Province = add.AdminArea;
                place.ZipCode  = add.PostalCode;
                place.Country  = add.CountryCode;
                return(place);
            }

            return(null);
        }
示例#15
0
        public async Task <LocationAddress> GetLocationAddress(double latitude, double longitude)
        {
            var geoCoder  = new Geocoder(Android.App.Application.Context);
            var addresses = await geoCoder.GetFromLocationAsync(latitude, longitude, 1);

            if (addresses.Any())
            {
                var address = addresses.First();

                var location = new LocationAddress()
                {
                    Name        = address.FeatureName,
                    City        = address.Locality,
                    Province    = address.AdminArea,
                    ZipCode     = address.PostalCode,
                    CountryCode = address.CountryCode.ToLower(),
                    Country     = $"{address.CountryName} ({address.CountryCode})",
                    Address     = address.MaxAddressLineIndex > 0 ? address.GetAddressLine(0) : string.Empty
                };

                return(location);
            }

            return(null);
        }
示例#16
0
        public static async Task <Address> LocationAddress(double latitude, double longitude)
        {
            Geocoder        geocoder    = new Geocoder(Application.Context);
            IList <Address> addressList =
                await geocoder.GetFromLocationAsync(latitude, longitude, 1);

            return(addressList.FirstOrDefault());
        }
示例#17
0
        // Example call: Address address = await LocationInformation.ReverseGeocodeCurrentLocation(this, location);
        public static async Task <Address> ReverseGeocodeCurrentLocation(Context parent, Location location)
        {
            Geocoder        geocoder    = new Geocoder(parent);
            IList <Address> addressList = await geocoder.GetFromLocationAsync(location.Latitude, location.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
示例#18
0
        //Reverse geocode : convert Locaton to name
        public async Task <Address> ReverseGeoCode(MainActivity mainActivity, Context ctx, SharedPartnerDataModels.Location loc)
        {
            Geocoder        geocoder    = new Geocoder(ctx);
            IList <Address> addressList = await geocoder.GetFromLocationAsync(loc.lat, loc.lng, 5);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
        public async Task <IEnumerable <Address> > GetAddress2(double lat, double lng)
        {
            var globals  = Mvx.Resolve <IMvxAndroidGlobals>();
            var geocoder = new Geocoder(globals.ApplicationContext);

            var addresses = await geocoder.GetFromLocationAsync(lat, lng, 1);

            return(addresses.Select(Convert));
        }
        async void AddressButton_OnClick(object sender, EventArgs eventArgs)
        {
            Geocoder        geocoder    = new Geocoder(this);
            IList <Address> addressList = await geocoder.GetFromLocationAsync(lat, longi, 10);

            Address address = addressList.FirstOrDefault();

            DisplayAddress(address);
        }
示例#21
0
        async System.Threading.Tasks.Task <Address> ReverseGeocodeCurrentLocation()
        {
            Geocoder        geocoder    = new Geocoder(this);
            IList <Address> addressList = await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
示例#22
0
        private async void OnLocationChanged()
        {
            try
            {
                // Creating a marker
                MarkerOptions markerOptions = new MarkerOptions();

                LatLng latLng = new LatLng(Lat, Lng);
                // Setting the position for the marker
                markerOptions.SetPosition(latLng);
                markerOptions.SetTitle(GetText(Resource.String.Lbl_Title_Location));

                #pragma warning disable 618
                var locale = (int)Build.VERSION.SdkInt < 25 ? Resources?.Configuration?.Locale : Resources?.Configuration?.Locales.Get(0) ?? Resources?.Configuration?.Locale;
                #pragma warning restore 618
                Geocoder geocode = new Geocoder(this, locale);

                var addresses = await geocode.GetFromLocationAsync(latLng.Latitude, latLng.Longitude, 2); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                switch (addresses?.Count)
                {
                case > 0:
                    DeviceAddress = addresses[0].GetAddressLine(0);     // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
                    //string city = addresses[0].Locality;
                    //string state = addresses[0].AdminArea;
                    //string country = addresses[0].CountryName;
                    //string postalCode = addresses[0].PostalCode;
                    //string knownName = addresses[0].FeatureName; // Only if available else return NULL

                    // Setting the title for the marker.
                    // This will be displayed on taping the marker
                    markerOptions.SetSnippet(DeviceAddress);
                    break;
                }

                if (Map != null)
                {
                    Map.Clear();

                    Map.AddMarker(markerOptions);
                    //Map.SetOnInfoWindowClickListener(this); // Add event click on marker icon
                    Map.MapType = GoogleMap.MapTypeNormal;

                    var builder = CameraPosition.InvokeBuilder();
                    builder.Target(new LatLng(Lat, Lng));
                    var cameraPosition = builder.Zoom(17).Target(new LatLng(Lat, Lng)).Build();
                    cameraPosition.Zoom = 18;

                    var cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);
                    Map.MoveCamera(cameraUpdate);
                }
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
            }
        }
示例#23
0
        public async Task <Address> ReverseGeoCode(Context ctx, double Latitude, double Longitude)
        {
            Geocoder        geocoder    = new Geocoder(ctx);
            IList <Address> addressList = await geocoder.GetFromLocationAsync(Latitude, Longitude, 5);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
示例#24
0
        public async Task <IEnumerable <Placemark> > GetPlacemarksAsync(double latitude, double longitude)
        {
            using (var geocoder = new Geocoder(Platform.AppContext))
            {
                var addressList = await geocoder.GetFromLocationAsync(latitude, longitude, 10);

                return(addressList?.ToPlacemarks());
            }
        }
示例#25
0
        async private Task <Address> ReverseGeocodeCurrentLocation()
        {
            Geocoder        geocoder    = new Geocoder(this);
            IList <Address> addressList =
                await geocoder.GetFromLocationAsync(CurrentLocation.Latitude, CurrentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
示例#26
0
        async Task <Address> ReverseGeocodeCurrentLocation()
        {
            Geocoder        geo         = new Geocoder(this);
            IList <Address> addressList =
                await geo.GetFromLocationAsync(Lat, Long, 10);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
示例#27
0
        public static async Task <Address> ReverseGeocodeCurrentLocation(LatLng point, Context context)
        {
            var geocoder = new Geocoder(context);

            IList <Address> addressList = await geocoder.GetFromLocationAsync(point.Latitude, point.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            return(address);
        }
示例#28
0
        public async void GetAddress(Location location)
        {
            Geocoder geocoder = new Geocoder(this);

            IList <Address> addressList = await geocoder.GetFromLocationAsync
                                              (location.Latitude, location.Longitude, 2);

            Address address = addressList.FirstOrDefault();

            DisplayAddress(address);
        }
示例#29
0
        private async Task <Address> ReverseGeocodeCurrentLocation()
        {
            Geocoder        geocoder    = new Geocoder(this);
            IList <Address> addressList =
                await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

            Address address = addressList.FirstOrDefault();

            Console.Out.WriteLine(address.ToString());
            return(address);
        }
示例#30
0
        public async Task <string> GetCountryFromPosition(double latitude, double longitude)
        {
            var geo       = new Geocoder(Android.App.Application.Context);
            var addresses = await geo.GetFromLocationAsync(latitude, longitude, 1);

            if (addresses.Any())
            {
                return(addresses[0].CountryCode);
            }
            return(null);
        }