'Xamarin Getting User position Coordinates and passing to another method
I'm able to get the coordinates from the user using Geolocation from within async Task method. I would like to know how I could get the 'location.Lattitude' & 'location.Longitude' values and pass them to a global variable in order to use them in another method? Whenever I tried before I keep getting null for both the values. Thank you for any assistance.
public async Task GetUserPosition()
{
try
{
var policeClient = new PoliceUkClient();
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
Posi = new Position(location.Latitude, location.Longitude);
area = new Geoposition(location.Latitude, location.Longitude);
setLattitude = location.Latitude;
setLongitude = location.Longitude;
MapSpan mapSpan = MapSpan.FromCenterAndRadius(Posi, Distance.FromKilometers(.444));
customMap.MoveToRegion(mapSpan);
}
}
catch (Exception Ex)
{
//Do nothing
}
}
Update - Added more information and updated the GetUserPositionMethod() to show how I'm trying to pass values to use in another method.
The code works fine as soon as the app opens it will move the map to the user location. I declared the following as public statics
public static double setLattitude;
public static double setLongitude;
public static Position Posi;
public static Geoposition area;
public static StreetLevelCrimeResults results;
I pass the lattitude & longitude variables to 'setLattitude' & 'setLongitude' thinking I could then use the values in another method but its always returning as null. If I add a breakpoint for the line 'StreetLevelCrimeResults results = policeClient.StreetLevelCrimes(currentarea);' it will be null. I'm hoping I haven't missed something silly with this.
public void test()
{
double test3 = 51.5736083445471;
double test4 = -2.95297576889984;
var test1 = setLattitude;
var test2 = setLongitude;
var policeClient = new PoliceUkClient();
Geoposition currentarea = new Geoposition(setLattitude, setLongitude);
//custom pins works
//Geoposition area = new Geoposition(test3, test4);
//not working as always null
//Geoposition area = new Geoposition(test1, test2);
StreetLevelCrimeResults results = policeClient.StreetLevelCrimes(currentarea);
try
{
List<CustomPin> custompinList = new List<CustomPin>();
foreach (Crime crime in results.Crimes)
{
if (crime.Category == "public-order")
{
pin = new CustomPin()
{
Type = PinType.Place,
Position = new Position(crime.Location.Latitude, crime.Location.Longitude),
//Position = new Position(51.589177, -2.998055),
//Position = new Position(51.591676, -3.005573),
Label = crime.Category,
Address = "Location: " + crime.Location.Street.Name,
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};
custompinList.Add(pin);
customMap.Pins.Add(pin);
//customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(51.589177, -2.998055), Distance.FromMiles(1.0)));
}
}
customMap.CustomPins = custompinList;
}
catch (Exception ex)
{
//handler error (Log?)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
