You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi
Visual Studio Preview 2022 .net 9 MAUI
Cant find using CoreLocation; using MapKit; using Foundation; so CLLocation(), MKLocalSearchRequest and MKCoordinateRegion() etc does not work
Any Idea ?
Thank you Martin
see code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Devices.Sensors;
#if IOS || MACCATALYST
using CoreLocation;
using MapKit;
using Foundation;
#endif
namespace LocationServiceNamespace
{
public partial class LocationService
{
String LocationName;
public LocationService()
{
_ = GetLocationAsync(); // Added discard operator to handle the async call
}
public async Task<string> GetLocationAsync()
{
string LocationName;
LocationName = string.Empty; // Initialize to an empty string;
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location == null)
{
location = await Geolocation.GetLocationAsync(new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.Medium,
Timeout = TimeSpan.FromSeconds(30)
});
}
if (location != null)
{
LocationName = $"Latitude: {location.Latitude}, Longitude: {location.Longitude}";
}
else
{
LocationName = "No location data available";
}
}
catch (FeatureNotSupportedException)
{
// Handle not supported on device exception
LocationName = "Location not supported on this device";
}
catch (PermissionException)
{
// Handle permission exception
LocationName = "Location permission denied";
}
catch (Exception)
{
// Unable to get location
LocationName = "Unable to get location";
}
return LocationName;
}
public async Task<string> FindPlaceAsync(string query)
{
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location == null)
{
location = await Geolocation.GetLocationAsync(new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.Medium,
Timeout = TimeSpan.FromSeconds(30)
});
}
if (location != null)
{
#if IOS || MACCATALYST
var clLocation = new CLLocation(location.Latitude, location.Longitude);
var request = new MKLocalSearchRequest
{
NaturalLanguageQuery = query,
Region = new MKCoordinateRegion(clLocation.Coordinate, new MKCoordinateSpan(0.1, 0.1))
};
var search = new MKLocalSearch(request);
var response = await search.StartAsync();
var nearestPlace = response.MapItems.OrderBy(item => item.Placemark.Location.DistanceFrom(clLocation)).FirstOrDefault();
if (nearestPlace != null)
{
var result = $"Place Name: {nearestPlace.Name}, Address: {nearestPlace.Placemark.Title}";
return result;
}
else
{
return "No places found nearby.";
}
#else
return "Apple Maps is not supported on this platform.";
#endif
}
else
{
return "No location data available.";
}
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi
Visual Studio Preview 2022 .net 9 MAUI
Cant find using CoreLocation; using MapKit; using Foundation; so CLLocation(), MKLocalSearchRequest and MKCoordinateRegion() etc does not work
Any Idea ?
Thank you Martin
see code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Devices.Sensors;
#if IOS || MACCATALYST
using CoreLocation;
using MapKit;
using Foundation;
#endif
namespace LocationServiceNamespace
{
public partial class LocationService
{
String LocationName;
#if IOS || MACCATALYST
var clLocation = new CLLocation(location.Latitude, location.Longitude);
var request = new MKLocalSearchRequest
{
NaturalLanguageQuery = query,
Region = new MKCoordinateRegion(clLocation.Coordinate, new MKCoordinateSpan(0.1, 0.1))
};
#else
return "Apple Maps is not supported on this platform.";
#endif
}
else
{
return "No location data available.";
}
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions