static Task <IEnumerable <string> > GetAddressesForPositionAsync(Position position) { var location = new CLLocation(position.Latitude, position.Longitude); var geocoder = new CCLGeocoder(); var source = new TaskCompletionSource <IEnumerable <string> >(); geocoder.ReverseGeocodeLocation(location, (placemarks, error) => { if (placemarks == null) { placemarks = new CLPlacemark[0]; } List <string> addresses = new List <string>(); #if __MOBILE__ && !(MACCATALYST || MACOS || __MACCATALYST__) #pragma warning disable BI1234, CA1416 // Type or member is obsolete, ABAddressFormatting.ToString(...) has [UnsupportedOSPlatform("ios9.0")] addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)).ToList(); #pragma warning restore BI1234, CA1416 // Type or member is obsolete #else foreach (var item in placemarks) { var address = new CNMutablePostalAddress(); #pragma warning disable CA1416 // TODO: 'CLPlacemark.AddressDictionary' is unsupported on: 'maccatalyst' 11.0 and later address.Street = item.AddressDictionary["Street"] == null ? "" : item.AddressDictionary["Street"].ToString(); address.State = item.AddressDictionary["State"] == null ? "" : item.AddressDictionary["State"].ToString(); address.City = item.AddressDictionary["City"] == null ? "" : item.AddressDictionary["City"].ToString(); address.Country = item.AddressDictionary["Country"] == null ? "" : item.AddressDictionary["Country"].ToString(); address.PostalCode = item.AddressDictionary["ZIP"] == null ? "" : item.AddressDictionary["ZIP"].ToString(); #pragma warning restore CA1416 addresses.Add(CNPostalAddressFormatter.GetStringFrom(address, CNPostalAddressFormatterStyle.MailingAddress)); } #endif source.SetResult(addresses); }); return(source.Task); }
public override void ViewDidLoad() { base.ViewDidLoad(); locMan = new CLLocationManager(); locMan.RequestWhenInUseAuthorization(); locMan.RequestAlwaysAuthorization(); // Geocode a city to get a CLCircularRegion, // and then use our location manager to set up a geofence button.TouchUpInside += (o, e) => { // clean up monitoring of old region so they don't pile up if (region != null) { locMan.StopMonitoring(region); } // Geocode city location to create a CLCircularRegion - what we need for geofencing! var taskCoding = geocoder.GeocodeAddressAsync("Cupertino"); taskCoding.ContinueWith((addresses) => { CLPlacemark placemark = addresses.Result [0]; region = (CLCircularRegion)placemark.Region; locMan.StartMonitoring(region); }); }; // This gets called even when the app is in the background - try it! locMan.RegionEntered += (sender, e) => { Console.WriteLine("You've entered the region"); }; locMan.RegionLeft += (sender, e) => { Console.WriteLine("You've left the region"); }; }
static Task <IEnumerable <string> > GetAddressesForPositionAsync(Position position) { var location = new CLLocation(position.Latitude, position.Longitude); var geocoder = new CCLGeocoder(); var source = new TaskCompletionSource <IEnumerable <string> >(); geocoder.ReverseGeocodeLocation(location, (placemarks, error) => { if (placemarks == null) { placemarks = new CLPlacemark[0]; } List <string> addresses = new List <string>(); #if __MOBILE__ addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)).ToList(); #else foreach (var item in placemarks) { var address = new CNMutablePostalAddress(); address.Street = item.AddressDictionary["Street"] == null ? "" : item.AddressDictionary["Street"].ToString(); address.State = item.AddressDictionary["State"] == null ? "" : item.AddressDictionary["State"].ToString(); address.City = item.AddressDictionary["City"] == null ? "" : item.AddressDictionary["City"].ToString(); address.Country = item.AddressDictionary["Country"] == null ? "" : item.AddressDictionary["Country"].ToString(); address.PostalCode = item.AddressDictionary["ZIP"] == null ? "" : item.AddressDictionary["ZIP"].ToString(); addresses.Add(CNPostalAddressFormatter.GetStringFrom(address, CNPostalAddressFormatterStyle.MailingAddress)); } #endif source.SetResult(addresses); }); return(source.Task); }
internal static Location ToLocation(this CLPlacemark placemark) => new Location { Latitude = placemark.Location.Coordinate.Latitude, Longitude = placemark.Location.Coordinate.Longitude, TimestampUtc = DateTimeOffset.UtcNow };
private GeoAddress ConvertPlacemarkToAddress(CLPlacemark placemark) { var streetNumber = ConvertStreetNumberRangeToSingle(placemark.SubThoroughfare); var fullAddress = ConvertAddressDictionaryToFullAddress(placemark.AddressDictionary); // replace corrected street number in the full address if (streetNumber.HasValue()) { fullAddress = fullAddress.Replace(placemark.SubThoroughfare, streetNumber); } var geoAddress = new GeoAddress { StreetNumber = streetNumber, Street = placemark.Thoroughfare, Latitude = placemark.Location.Coordinate.Latitude, Longitude = placemark.Location.Coordinate.Longitude, City = placemark.Locality ?? placemark.SubLocality, FullAddress = fullAddress, State = placemark.AdministrativeArea, ZipCode = placemark.PostalCode }; return(geoAddress); }
private static Address Convert(CLPlacemark placemark) { string addressLine = null; if (placemark.AddressDictionary.TryGetValue(ABPersonAddressKey.Street, out var value)) { addressLine = (NSString)value; } return(new Address { Name = placemark.Name, Latitude = placemark.Location.Coordinate.Latitude, Longitude = placemark.Location.Coordinate.Longitude, Country = placemark.Country, PostalCode = placemark.PostalCode, Locality = placemark.Locality, SubLocality = placemark.SubLocality, Thoroughfare = placemark.Thoroughfare, SubThoroughfare = placemark.SubThoroughfare, AdministrativeArea = placemark.AdministrativeArea, SubAdministrativeArea = placemark.SubAdministrativeArea, AddressLine = addressLine, }); }
public TravelMapLocationDataSource() { locationCheckTimer = new Timer(3000); locationCheckTimer.Elapsed += (object sender, ElapsedEventArgs e) => { Console.WriteLine($"Tick, updating {Locations.Count} items."); foreach (var locale in Locations) { CLGeocoder coder = new CLGeocoder(); CLLocation clLocation = new CLLocation(locale.Location.Latitude, locale.Location.Longitude); locale.LocationNameString = $"{locale.Location.Latitude}, {locale.Location.Longitude}"; coder.ReverseGeocodeLocation(clLocation, (placemarks, error) => { CLPlacemark placemark = placemarks[0]; if (placemark != null) { locale.LocationNameString = placemark.Name; } if (error != null) { Console.WriteLine("Error Placemarking: " + error.LocalizedDescription); locale.LocationNameString = $"{locale.Location.Latitude}, {locale.Location.Longitude}."; } }); RaiseLocationNameAcquired(this, new EventArgs()); } }; locationCheckTimer.Start(); }
private string FormatSeekiosAdress(CLPlacemark placemark) { StringBuilder result = new StringBuilder(); if (!string.IsNullOrEmpty(placemark.SubThoroughfare)) { result.Append(placemark.SubThoroughfare + ", "); } if (!string.IsNullOrEmpty(placemark.Thoroughfare)) { result.Append(placemark.Thoroughfare + ", "); } if (!string.IsNullOrEmpty(placemark.PostalCode)) { result.Append(placemark.PostalCode + ", "); } if (!string.IsNullOrEmpty(placemark.Locality)) { result.Append(placemark.Locality + ", "); } if (!string.IsNullOrEmpty(placemark.AdministrativeArea)) { result.Append(placemark.AdministrativeArea + ", "); } result.Append(placemark.Country); return(result.ToString()); }
internal static Location ToLocation(this CLPlacemark placemark) => new Location { Latitude = placemark.Location.Coordinate.Latitude, Longitude = placemark.Location.Coordinate.Longitude, Altitude = placemark.Location.Altitude, AltitudeReferenceSystem = AltitudeReferenceSystem.Geoid, Timestamp = DateTimeOffset.UtcNow };
// Constructs a `CNMutablePostalAddress` from a `CLPlacemark` static CNMutablePostalAddress CreatePostalAddress(CLPlacemark placemark) { return(new CNMutablePostalAddress { Street = string.Format("{0} {1}", placemark.SubThoroughfare ?? string.Empty, placemark.Thoroughfare ?? string.Empty), City = placemark.Locality ?? string.Empty, State = placemark.AdministrativeArea ?? string.Empty, PostalCode = placemark.PostalCode ?? string.Empty, Country = placemark.Country ?? string.Empty }); }
static Task<IEnumerable<Position>> GetPositionsForAddressAsync(string address) { var geocoder = new CLGeocoder(); var source = new TaskCompletionSource<IEnumerable<Position>>(); geocoder.GeocodeAddress(address, (placemarks, error) => { if (placemarks == null) placemarks = new CLPlacemark[0]; IEnumerable<Position> positions = placemarks.Select(p => new Position(p.Location.Coordinate.Latitude, p.Location.Coordinate.Longitude)); source.SetResult(positions); }); return source.Task; }
static Task<IEnumerable<string>> GetAddressesForPositionAsync(Position position) { var location = new CLLocation(position.Latitude, position.Longitude); var geocoder = new CLGeocoder(); var source = new TaskCompletionSource<IEnumerable<string>>(); geocoder.ReverseGeocodeLocation(location, (placemarks, error) => { if (placemarks == null) placemarks = new CLPlacemark[0]; IEnumerable<string> addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)); source.SetResult(addresses); }); return source.Task; }
static Task <IEnumerable <Position> > GetPositionsForAddressAsync(string address) { var geocoder = new CCLGeocoder(); var source = new TaskCompletionSource <IEnumerable <Position> >(); geocoder.GeocodeAddress(address, (placemarks, error) => { if (placemarks == null) { placemarks = new CLPlacemark[0]; } IEnumerable <Position> positions = placemarks.Select(p => new Position(p.Location.Coordinate.Latitude, p.Location.Coordinate.Longitude)); source.SetResult(positions); }); return(source.Task); }
static void inicializaGeolocalizacion() { /*Se crean las variables de geolocalizacion*/ CLGeocoder geocoder = new CLGeocoder(); //CLCircularRegion region;19.285116, -99.675914 CLCircularRegion region = new CLCircularRegion(new CLLocationCoordinate2D(+19.285116, -99.675914), 100129.46, "Casa de toño");//19.273600, -99.675620 CLLocationManager locMan; /*Se crean las variables de geolocalizacion*/ locMan = new CLLocationManager(); locMan.RequestWhenInUseAuthorization(); locMan.RequestAlwaysAuthorization(); // Geocode a city to get a CLCircularRegion, // and then use our location manager to set up a geofence // clean up monitoring of old region so they don't pile up Console.Write("Soy la region"); Console.Write(region); Console.Write("termino soy la region"); if (region != null) { locMan.StopMonitoring(region); } // Geocode city location to create a CLCircularRegion - what we need for geofencing! var taskCoding = geocoder.GeocodeAddressAsync("Cupertino"); taskCoding.ContinueWith((addresses) => { CLPlacemark placemark = addresses.Result[0]; region = (CLCircularRegion)placemark.Region; Console.Write("\nInicio el monitoreo .........."); locMan.StartMonitoring(region); Console.Write("\nTermino el monitoreo .........."); }); // This gets called even when the app is in the background - try it! locMan.RegionEntered += (sender, e) => { Console.WriteLine("You've entered the region"); }; locMan.RegionLeft += (sender, e) => { Console.WriteLine("You've left the region"); }; }
static Task <IEnumerable <string> > GetAddressesForPositionAsync(Position position) { var location = new CLLocation(position.Latitude, position.Longitude); var geocoder = new CLGeocoder(); var source = new TaskCompletionSource <IEnumerable <string> >(); geocoder.ReverseGeocodeLocation(location, (placemarks, error) => { if (placemarks == null) { placemarks = new CLPlacemark[0]; } IEnumerable <string> addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)); source.SetResult(addresses); }); return(source.Task); }
IEnumerable<Address> OnCompletion(CLPlacemark[] placemarks) { var matches = new List<Address>(); foreach(var placemark in placemarks) { matches.Add(new Address { Description = placemark.Name, Street = placemark.AddressDictionary.ValueForKey(ABPersonAddressKey.Street).ToString(), City = placemark.AddressDictionary.ValueForKey(ABPersonAddressKey.City).ToString(), State = placemark.AddressDictionary.ValueForKey(ABPersonAddressKey.State).ToString(), PostalCode = placemark.AddressDictionary.ValueForKey(ABPersonAddressKey.Zip).ToString(), Country = placemark.AddressDictionary.ValueForKey(ABPersonAddressKey.Country).ToString(), Latitude = placemark.Location.Coordinate.Latitude, Longitude = placemark.Location.Coordinate.Longitude, }); } return matches; }
private void HandleGeocodeCompletion(CLPlacemark[] placemarks, NSError error) { string newAddress = ""; if (error == null) { if (placemarks.Length > 0 && placemarks != null) { CLPlacemark placemark = placemarks[0]; // placemarks[placemarks.Length - 1]; newAddress = placemark.AddressDictionary.Values[0].ToString(); } else { newAddress = "Geocoding returned empty dataset..."; } Interlocked.Exchange <string>(ref thisDeviceAddress, newAddress); } else { newAddress = String.Format("Geocoding error: {0}", error.Code); Interlocked.Exchange <string>(ref thisDeviceAddress, newAddress); } var CurrentRequest = RequestQueue[0]; string checkPointMessage = String.Format("{0} {1}: Job data input finished: Run={2}, CN={3}, Job={4}, Lat={5}, Lng={6}, Address={7}", CurrentRequest.timeStamp.Date.ToString("yyyy-MM-dd"), CurrentRequest.timeStamp.ToString("HH:mm:ss"), MyConstants.EmployeeID, CurrentRequest.customer, CurrentRequest.job, CurrentRequest.lat, CurrentRequest.lng, newAddress); // MonoTouch.TestFlight.TestFlight.PassCheckpoint (checkPointMessage); AddLocationToBuffer(CurrentRequest.lng, CurrentRequest.lat, CurrentRequest.timeStamp.ToString("yyyy-MM-dd HH:mm:ss"), "Job data input finished: " + newAddress, CurrentRequest.customer, CurrentRequest.job); RequestQueue.RemoveAt(0); if (RequestQueue.Count > 0) { geo.ReverseGeocodeLocation(new CLLocation(RequestQueue[0].lat, RequestQueue[0].lng), HandleGeocodeCompletion); } }
public override void LocationsUpdated(CLLocationManager manager, CLLocation[] locations) { var geocoder = new CLGeocoder(); var newLocation = locations[0]; // TODO see if the anonymous function causes a memory leak geocoder.ReverseGeocodeLocation(newLocation, new CLGeocodeCompletionHandler((placemarks, error) => { if (placemarks.Length < 1) { return; } CLPlacemark placemark = placemarks[0]; Location location = new Location(newLocation.Coordinate.Latitude, newLocation.Coordinate.Longitude, placemark.Locality, placemark.SubLocality); CurrentLocation = location; Handler.OnLocationChanged(location); })); }
/// <summary> /// Purpose: Draw the first placemark in the mapview with the person name and street -> zoom the camera at the location /// </summary> /// <param name="placemarks">Placemarks.</param> /// <param name="error">Error.</param> void HandleCLGeocodeCompletionHandler (CLPlacemark[] placemarks, NSError error) { CLLocationCoordinate2D loc; // if GeoCode hasn't found a placemark, leave the function if (placemarks == null) return; // Delete the previous annotation if (_MainAnnotation != null) MapView.RemoveAnnotation(_MainAnnotation); // Assign the class annotation with the received values loc = placemarks[0].Location.Coordinate; _MainAnnotation = new MKPointAnnotation(){ Title = _person.Name ,Subtitle = _person.Strasse }; _MainAnnotation.SetCoordinate(new CLLocationCoordinate2D (loc.Latitude,loc.Longitude )); // Add the annotation to the mapview MapView.AddAnnotation(_MainAnnotation); var region = new MKCoordinateRegion (_MainAnnotation.Coordinate,new MKCoordinateSpan (1f / 0.5f, 1f / 0.5f)); // MapView.SetRegion (region, true); }
/// <summary> /// Navigate to an address /// </summary> /// <param name="name">Label to display</param> /// <param name="street">Street</param> /// <param name="city">City</param> /// <param name="state">Sate</param> /// <param name="zip">Zip</param> /// <param name="country">Country</param> /// <param name="countryCode">Country Code if applicable</param> /// <param name="navigationType">Navigation type</param> public async void NavigateTo(string name, string street, string city, string state, string zip, string country, string countryCode, NavigationType navigationType = NavigationType.Default) { if (string.IsNullOrWhiteSpace(name)) { name = string.Empty; } if (string.IsNullOrWhiteSpace(street)) { street = string.Empty; } if (string.IsNullOrWhiteSpace(city)) { city = string.Empty; } if (string.IsNullOrWhiteSpace(state)) { state = string.Empty; } if (string.IsNullOrWhiteSpace(zip)) { zip = string.Empty; } if (string.IsNullOrWhiteSpace(country)) { country = string.Empty; } var placemarkAddress = new MKPlacemarkAddress { City = city, Country = country, State = state, Street = street, Zip = zip, CountryCode = countryCode }; var coder = new CLGeocoder(); var placemarks = await coder.GeocodeAddressAsync(placemarkAddress.Dictionary); if (placemarks.Length == 0) { Debug.WriteLine("Unable to get geocode address from address"); return; } CLPlacemark placemark = placemarks[0]; var mapItem = new MKMapItem(new MKPlacemark(placemark.Location.Coordinate, placemarkAddress)); mapItem.Name = name; MKLaunchOptions launchOptions = null; if (navigationType != NavigationType.Default) { launchOptions = new MKLaunchOptions { DirectionsMode = navigationType == NavigationType.Driving ? MKDirectionsMode.Driving : MKDirectionsMode.Walking }; } var mapItems = new[] { mapItem }; MKMapItem.OpenMaps(mapItems, launchOptions); }
void HandleCLGeocodeCompletionHandler (CLPlacemark[] placemarks, NSError error) { try { var placeMark = placemarks [0]; Console.WriteLine(placeMark); LocationUpdated (this, new LocationUpdatedEventArgs (placeMark)); } catch (Exception ex) { var erroralert = new UIAlertView ("Location could not be found", "", null, "Ok", null); erroralert.Show (); } }
public LocationUpdatedEventArgs(CLPlacemark placeMark) { this.placeMark = placeMark; }
internal static Location ToLocation(this CLPlacemark placemark) => new Location(placemark.Location.Coordinate.Latitude, placemark.Location.Coordinate.Longitude, DateTimeOffset.UtcNow);
// Constructs a `CNMutablePostalAddress` from a `CLPlacemark` static CNMutablePostalAddress CreatePostalAddress (CLPlacemark placemark) { return new CNMutablePostalAddress { Street = string.Format ("{0} {1}", placemark.SubThoroughfare ?? string.Empty, placemark.Thoroughfare ?? string.Empty), City = placemark.Locality ?? string.Empty, State = placemark.AdministrativeArea ?? string.Empty, PostalCode = placemark.PostalCode ?? string.Empty, Country = placemark.Country ?? string.Empty }; }
public void ReverseGeocodeLocationHandle(CLPlacemark[] placemarks, NSError error) { Location loc = new Location(); for (int i = 0; i < placemarks.Length; i++) { loc.Zipcode = placemarks [i].PostalCode; loc.City = placemarks[i].Locality; loc.State = placemarks[i].AdministrativeArea; } if (screen.CurrentWeather == null) { screen.CurrentWeather = WeatherService.CurrentWeather(loc.Zipcode); screen.CurrentLocation = loc; screen.DisplayScreen(); } }