how to get the current gps coordinates in xamarin - c#

I would like to get the user's current latitude and longitude for ios in xamarin.
I've looked at other posts and as i understand it.
it's like:
CLLocationManager lm = new CLLocationManager();
... (Acurray)
... (Other Specs)
lm.StartUpdatingLocation();
lm.Location.Coordinate.Latitude.ToString();
lm.Location.Coordinate.Longitude.ToString();
However, when i run it in the simulator it doesn't do anything. It doesn't ask me to turn on my location services or update my Label with the latitude and longitude.
Also, how do i make it check it location services are turned on and if they're not. How to make the user do it. (like the pop up on some apps asking you to turn on your gps)

CLLocationManger is async - it will fire events when the location is updated.
CLLocationManager lm = new CLLocationManager(); //changed the class name
... (Acurray)
... (Other Specs)
lm.LocationsUpdated += delegate(object sender, CLLocationsUpdatedEventArgs e) {
foreach(CLLocation l in e.Locations) {
Console.WriteLine(l.Coordinate.Latitude.ToString() + ", " +l.Coordinate.Longitude.ToString());
}
};
lm.StartUpdatingLocation();

Related

Bluetooth Fit Watch Connection Xamarin Android

i recently posted about connection to bluetooth devices using Xamarin. I managed to get the device and the mac address on a list. Im having a problem connecting to the device. It doesnt do anything when clicked... What are the next steps to pairing with device and is it even possible to pair with a fit watch, im trying to use the heart rate monitor on the fit watch as well.
I'm guessing the next step would be to setup a Click event on the list...
Something like
myListView = FindViewById<ListView>(Resource.Id.list);
myListView.ItemClick += List_Click;
private void List_Click(object sender, AdapterView.ItemClickEventArgs e)
{
//throw new NotImplementedException();
}
and is it possible to get the data from the fit watch and sync it with a chart.
I have managed to get a pie chart . now i just need to pair the fit watch and get the data from bluetooth. Any advice would be grateful thanks
You could get a BluetoothDevice object from the BluetoothAdapter
and something like :
myListView.ItemClick += List_Click;
private void List_Click(object sender, AdapterView.ItemClickEventArgs e)
{
var address = xxxxx; //the address you select
BluetoothDevice btDevice = mBluetoothAdapter.GetRemoteDevice(address);
var _socket = btDevice .CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
_socket.Connect();
}
Much of the specifics for handling paired devices is in the documentation

Basic C# WPF Program - Converting string to double then adding variables in Textblock

Good evening all,
I've just started my second year at University and I'm learning C# for the first time. I've had some coding experience with Java and Javascript before but never c#.
I'm having problems with a basic credit/debit program. See code below:
private double balance = 0;
private string creditamount;
private string debitamount;
private double currentamount = 0;
private void CreditButton_Click(object sender, RoutedEventArgs e)
{
AmountField.Text = creditamount;
Convert.ToDouble(creditamount);
CurrentBalance.Text = "Your current balance is £" + (creditamount + balance);
}
private void DebitButton_Click(object sender, RoutedEventArgs e)
{
AmountField.Text = debitamount;
double.Parse(debitamount);
CurrentBalance.Text = "Your current balance is £" + (debitamount - balance);
}
The user should enter an amount into the AmountField and press either the Credit or Debit button. Upon clicking either button, the amount should be converted from a string into a double and then shown in the CurrrentBalance .Text
It apppears that my strings aren't being converted into doubles.
I've tried using Convert.ToDouble(); and double.Parse(); but Visual Studio keeps giving me errors.
Does anyone have any suggestions?
The = assignment goes from Right to Left. Also all Convert.ToStuff return the result, so you should be expecting it (assign it to some variable).
Therefore, your event handlers (and all your future routine) should look similar to this:
private void DebitButton_Click(object sender, RoutedEventArgs e)
{
debitamount= AmountField.Text ; // Take the Right and assign it to the Left
currentamount = Convert.ToDouble(debitamount); // convert + assign
CurrentBalance.Text = "Your current balance is £" + (debitamount - balance); // Again take the Right and assign it to the Left
}
First of all if you are using wpf try to avoid this code behind thing. Secondly try to take advantage of MVVM. What is MVVM. Go through this link.
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
So in short i would suggest create a Viewmodel class and make it implement INotifyPropertyChanged. Create your credit and debit amount properties bind it in the UI and don't forget to use OnPropertyChanged on these properties. So when ever user enters any text in the UI i.e when ever there is a change in the UI state your set block of the variables will be called and you can proceed further. Also in mvvm you use commands not button clicks. Also set the data context of you xaml to this view model.

How do I trigger click event to get the coordinates from the map in uiversal app

I trying to perform a seeimly simple task in a universal App.
I want to be able to click on my map and have that click trigger an event which will give me the coordinates.
There are many guides demonstrating how to do this but I need a solution that will work in my universal app (windows 8.1/WindowsPhone).
MSDN-blogger Ricky Brundritt have written my great posts on the subject and I have followed this guide in order to create a map that can be used in both projects.
http://blogs.msdn.com/b/rbrundritt/archive/2014/06/24/how-to-make-use-of-maps-in-universal-apps.aspx
If I understand correctly he creates a file with conditional statements that gets shared between the projects. And when you call the method the proper implementatoin gets used depending on which project is run:
E.x:
public void SetView(BasicGeoposition center, double zoom)
{
#if WINDOWS_APP
_map.SetView(center.ToLocation(), zoom);
OnPropertyChanged("Center");
OnPropertyChanged("Zoom");
#elif WINDOWS_PHONE_APP
_map.Center = new Geopoint(center);
_map.ZoomLevel = zoom;
#endif
}
Now, is there a way to implement a method that will give me the coordinates when i Click on a map?
Here is an answer to a similar question: (how to get the geolocation of a click on a bing map in C#)
public MainPage()
{
this.InitializeComponent();
this.MyMap.PointerPressedOverride += MyMap_PointerPressedOverride;
}
void MyMap_PointerPressedOverride(object sender, PointerRoutedEventArgs e)
{
Bing.Maps.Location l = new Bing.Maps.Location();
this.MyMap.TryPixelToLocation(e.GetCurrentPoint(this.MyMap).Position, out l);
Bing.Maps.Pushpin pushpin = new Bing.Maps.Pushpin();
pushpin.SetValue(Bing.Maps.MapLayer.PositionProperty, l);
this.MyMap.Children.Add(pushpin);
}
But this will not work for me beacuse cant get acess to;
this.MyMap.PointerPressedOverride += MyMap_PointerPressedOverride;
I only have:
this.MyMap.PointerPressed += MyMap_PointerPressedOverride;
I also dont have acess to TryPixelToLocation in
this.MyMap.TryPixelToLocation(e.GetCurrentPoint(this.MyMap).Position, out l);
SO to sum it up, im looking for a way to trigger an event when i click on the map that will work in both projects. Help appreciated.
Thank you!
EDIT:
#elif WINDOWS_PHONE_APP
private void _map_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Geopoint p;
_map.GetLocationFromOffset(e.GetCurrentPoint(_map).Position, out p);
MapClicked(p);
}
#endif
I have a strange problem with getting this to work on the phone. The method does not fire when i push on the map. When debugging on a device, you can see som numbers at the side of the screen. When I push where the numbers are the method fires and a gets added "behind" the numbers. This is really strange. Somehow the "numbers" are clickable but the map itself is not.
Have anyone had similar problems?
You can create an event handler in the map view control that returns the coordinate of where a user clicked on the map. You can then write the required code for the different map controls for handing the click events, getting the map coordinate, and passing it on to your event handler. Here is a modified constructor for the MapView control and some additional code that adds a MapClicked event and code that wraps the map controls and passes the map coordinate to the event.
public MapView()
{
#if WINDOWS_APP
_map = new Map();
_shapeLayer = new MapShapeLayer();
_map.ShapeLayers.Add(_shapeLayer);
_pinLayer = new MapLayer();
_map.Children.Add(_pinLayer);
_map.PointerPressedOverride += _map_PointerPressedOverride;
#elif WINDOWS_PHONE_APP
_map = new MapControl();
_map.PointerPressed += _map_PointerPressed;
#endif
this.Children.Add(_map);
SetMapBindings();
}
public delegate void MapClickHandler(Geopoint center);
/// <summary>
/// A callback method used to when the map is Clicked
/// </summary>
public event MapClickHandler MapClicked;
#if WINDOWS_APP
private void _map_PointerPressedOverride(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Location l;
_map.TryPixelToLocation(e.GetCurrentPoint(_map).Position, out l);
Geopoint p = new Geopoint(new BasicGeoposition()
{
Latitude = l.Latitude,
Longitude = l.Longitude
});
MapClicked(p);
}
#elif WINDOWS_PHONE_APP
private void _map_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Geopoint p;
_map.GetLocationFromOffset(e.GetCurrentPoint(_map).Position, out p);
MapClicked(p);
}
#endif

Event for Geolocation Changed in Windows Phone 8

Is there any event for the geolocation changed in Windows Phone 8 (C#)?
I want to trigger some event when the geolocation is changed like city from reversegeocoding changed.
If not, then is it possible to call some event manually whenever the Phone's location like City is Changed.
(suppose I have fetched the city using reverse geocoding). (for Windows Phone 8)
I can't understand your question properly, But this answer may solve your problem.
There is a event called "PositionChanged" in geolocator. This event gets triggered when geolocator position is changed.
geolocator = new Geolocator();
geolocator.PositionChanged -= geolocator_PositionChanged;
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00");
LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00");
});
}
for more info :::http://msdn.microsoft.com/en-us/library/windows/apps/jj247548(v=vs.105).aspx

Getting GPS Coordinates Windows Phone 7

I've been looking quite some time today to gather GPS coordinates from a Windows Phone 7 device - however since I do not have an actual test device here I tried to set some dummy data which I want to have returned instead of real GPS Data ... that, however ist not working out too well:
This code ist partially an example from so which I found here. However I tried to put it into a class which I can access later.
public class GetGPS : GeoCoordinateWatcher
{
GeoCoordinateWatcher watcher;
public GetGPS()
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.PositionChanged += this.watcher_PositionChanged;
watcher.StatusChanged += this.watcher_StatusChanged;
watcher.Start();
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Ready:
//plingpling
break;
case GeoPositionStatus.Disabled:
// location is unsupported on this device
break;
case GeoPositionStatus.NoData:
watcher.Position.Location.Latitude = 54.086369f;
watcher.Position.Location.Longitude = 12.124754f;
break;
}
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var epl = e.Position.Location;
// Access the position information thusly:
epl.Latitude.ToString("0.000");
epl.Longitude.ToString("0.000");
epl.Altitude.ToString();
epl.HorizontalAccuracy.ToString();
epl.VerticalAccuracy.ToString();
epl.Course.ToString();
epl.Speed.ToString();
e.Position.Timestamp.LocalDateTime.ToString();
}
}
This is my other class in which I try to access the data - however I always get NaN as lat1Rad and long1Rad ... can you please help me?
I want that example to be functional on the emulator ( with a fixed GPS Coordinate ) and on a phone 7 device - where it actually grabs the value.
GetGPS location1= new GetGPS();
//GeoCoordinate myPosition = location1.getPosition();
//Radianten berechnen
double lat1Rad = GradZuRad(location1.Position.Location.Latitude);
double long1Rad = GradZuRad(location1.Position.Location.Longitude);
I basically just want to program a class which returns me the CURRENT GPS Position.
Where is your example code from?
Have you tried using the sample on MSDN?
Alternatively, there's a greate simulator available from http://phone7.wordpress.com/2010/08/02/no-device-no-gps-no-matter-with-code/
Why are you deriving frmo GeoCoordinateWatcher? That's a mistake, IMO. It makes it unclear when you're using the members of your own class and when you're using the delegated instance. At the moment you're setting the coordinates on the delegated watcher, but then asking for the coordinates from the GetGPS instance directly.
I suggest you implement IGeoPositionWatcher<GeoCoordinate> with your own "fixed" position watcher - and then decide at execution time whether to use that or the real GeoCoordinateWatcher. Obviously this means your client code should only depend on IGeoPositionWatcher<GeoCoordinate> instead of GeoCoordinateWatcher directly. This should also help for unit testing purposes.
Of course Jon's answer is very apropos - I think you've not done reasonable interface abstraction, but even when you get that, for simulated data have you taken a look at Kevin Wolf's GPS Simulator for Windpws Phone?

Categories

Resources