/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async private void TraceLocationActivity_Click(object sender, EventArgs e) { try { longlat = new Coordinates(); if (_inputId.Text == "") { Toast.MakeText(this, "First Enter id to Trace Location", ToastLength.Long); } else { //Send ID to webRequest class to request server longlat.uniqueId = int.Parse(_inputId.Text); longlat = await WebRequestServer.TraceLastKnownLocation(longlat); if (longlat == null) { _showData.Text = string.Format("Latitude: " + "N\\A" + "\nLongitude: " + "N\\A" + "\nAccuracy: " + "N\\A" + "\nDatetime: " + "N\\A" + "\nSpeed: " + "N\\A" + "\nBearing: " + "N\\A" + "\nAltitude: " + "N\\A"); } else _showData.Text = string.Format("Latitude: " + longlat.Latitude + "\nLongitude: " + longlat.Longitude + "\nAccuracy: " + longlat.Accuracy + "\nDateTime: " + longlat.timeStamp + "\nSpeed: " + longlat.speed + "\nBearing: " + longlat.Bearing + "\nAltitude: " + longlat.altittude); } streetAddress(longlat); } catch (Exception ex) { throw; } }
/// <summary> /// Firsst Activity to run /// </summary> /// <param name="bundle"></param> protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); //Check for already stored preferences ISharedPreferences pref = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private); string uniqueId = pref.GetString("UniqueId", String.Empty); //If not then take it to login Activity if (uniqueId == string.Empty) { Intent intent = new Intent(this, typeof(LoginActivity)); this.StartActivity(intent); this.Finish(); } //If any then save it to coordinates class and send it to Main Activity else { Coordinates getUniqueId = new Coordinates { uniqueId = int.Parse(uniqueId) }; Intent intent = new Intent(this, typeof(GelLocation)); //Send preferences to main activity intent.PutExtra("user", JsonConvert.SerializeObject(getUniqueId)); this.StartActivity(intent); this.Finish(); } }
//http://gpsworld.com/what-exactly-is-gps-nmea-data/ static public GPS.Coordinates ParseNmeaToLonLat(string nmeaData, string nmeaType) { GPS.Coordinates coords = new GPS.Coordinates(); string[] srvrMsgs = nmeaData.Split(','); if (srvrMsgs [0] == nmeaType) { double Lat = Convert.ToDouble(srvrMsgs [2]) / 100; //32.226984 double Lon = Convert.ToDouble(srvrMsgs [4]) / 100; //86.186043 double Alt = Convert.ToDouble(srvrMsgs [9]); //Debug.Log("Lat: " + Lat); //Debug.Log("Lon: " + Lon); double Lat_deg = Convert.ToDouble(Math.Floor(Lat)); //32 double Lon_deg = Convert.ToDouble(Math.Floor(Lon)); //86 //Debug.Log("Lat_deg: " + Lat_deg); //Debug.Log("Lon_deg: " + Lon_deg); double Lat_minute = (Lat - Lat_deg) * 100 / 60; //(0.226984*100)/60 = (22.6984)/60 = 0.378306 double Lon_minute = (Lon - Lon_deg) * 100 / 60; //(0.186043*100)/60 = (18.6043)/60 = 0.310071 //Debug.Log("Lat_minute: " + Lat_minute); //Debug.Log("Lon_minute: " + Lon_minute); coords.lat = Lat_deg + Lat_minute; //32.378306 coords.lon = Lon_deg + Lon_minute; //86.310071 //Debug.Log("Lat_dec_deg: " + Lat_dec_deg); //Debug.Log("Lon_dec_deg: " + Lon_dec_deg); if (srvrMsgs [3] == "S") { coords.lat = coords.lat * -1; } if (srvrMsgs [5] == "W") { coords.lon = coords.lon * -1; } coords.alt = Alt; //Debug.Log ("Lat: " + Lat.ToString () + "\nLon: " + Lon.ToString () + "\n" + Alt.ToString ("0.0") + "MSL"); //gameObject.GetComponent<TextMesh> ().text = "Lat: " + Lat.ToString () + "\nLon: " + Lon.ToString () + "\n" + Alt.ToString ("0.0") + "MSL"; return(coords); } return(null); }
/// <summary> /// /// </summary> /// <param name="bundle"></param> async protected override void OnCreate(Bundle bundle) { try { base.OnCreate(bundle); //Get object of this class to show data in broadc GelLocation.instance = this; SetContentView(Resource.Layout.GetLocation); _locationText = FindViewById<TextView>(Resource.Id.currentLocationTextView); _addressText = FindViewById<TextView>(Resource.Id.addressTextView); _lastKnownLocation = FindViewById<TextView>(Resource.Id.lastLocationTextView); _lastTimeStamp = FindViewById<TextView>(Resource.Id.lastTwoTimeStamp); _listOfProvider = FindViewById<TextView>(Resource.Id.providerList); _traceLocationButton = FindViewById<Button>(Resource.Id.traceLocationButton); _idView = FindViewById<TextView>(Resource.Id.idView); _traceLocationButton.Click += _traceLocationButton_Click; //Get shared preferences data sharedPreferenceId = JsonConvert.DeserializeObject<Coordinates>(Intent.GetStringExtra("user")); //Show id _idView.Text = "User Id: " + sharedPreferenceId.uniqueId.ToString(); //Get device id for currentr user getDeviceId(); //Get last known location of current user latlon.uniqueId = sharedPreferenceId.uniqueId; latlon = await WebRequestServer.GetLastKnownLocation(latlon); //if respond is null if (latlon == null) { _lastKnownLocation.Text = string.Format("Latitude: " + "N\\A" + "\nLongitude: " + "N\\A" + "\nAccuracy: " + "N\\A" + "\nDatetime: " + "N\\A" + "\nBearing: " + "N\\A" + "\nAltitude: " + "N\\A"); } //Otherwise perform else _lastKnownLocation.Text = string.Format("Latitude: " + latlon.Latitude + "\nLongitude: " + latlon.Longitude + "\nAccuracy: " + latlon.Accuracy + "\nDateTime: " + latlon.timeStamp + "\nBearing: " + latlon.Bearing+ "\nAltitude: " + latlon.altittude); } catch (Exception ex) { throw; } }
/// <summary> /// Service broadcasting to activities along with data /// </summary> /// <param name="location"></param> /// <param name="InComingData"></param> private void BroadcastStarted(Coordinates location, string InComingData) { try { Intent BroadcastIntent = new Intent(this, typeof(GelLocation.MyLocationReceiver)); BroadcastIntent.SetAction(GelLocation.MyLocationReceiver.GRID_STARTED); BroadcastIntent.AddCategory(Intent.CategoryDefault); var cordinates = JsonConvert.SerializeObject(location); BroadcastIntent.PutExtra(InComingData, cordinates); SendBroadcast(BroadcastIntent); } catch (Exception ex) { Toast.MakeText(Application.Context, "Broadcast", ToastLength.Long).Show(); } }
private void _loginButton_Click(object sender, EventArgs e) { try { Coordinates getUniqueId = new Coordinates { //Save textbox id in object uniqueId = int.Parse(_uniqueId.Text) }; //If checkbox is enabled or disable then save data in shared preference if (_rememberMe.Checked || _rememberMe.Checked == false) { ISharedPreferences pref = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private); //Enable us to edit file ISharedPreferencesEditor edit = pref.Edit(); edit.PutString("UniqueId", _uniqueId.Text.Trim()); edit.Apply(); Intent intent = new Intent(this, typeof(GelLocation)); //Send preferences to main activity intent.PutExtra("user", JsonConvert.SerializeObject(getUniqueId)); this.StartActivity(intent); //User cannot navigate to this activity this.Finish(); } //Otherwise dont save in preferences only send data to main activity //else //{ // Intent intent = new Intent(this, typeof(GelLocation)); // //Send preferences to main activity // intent.PutExtra("user", JsonConvert.SerializeObject(getUniqueId)); // this.StartActivity(intent); // //User cannot navigate to this activity // this.Finish(); //} } catch (Exception ex) { throw; } }
// private static Uri ServerRootURL = new Uri("http://pggpstracker.azurewebsites.net/"); // private static string ServerLocation = "/api/location/"; /// <summary> /// /// </summary> /// <summary> /// Sending data in Json format to webservice /// </summary> /// <param name="longlat"></param> /// <returns></returns> async public static Task<string> SendingCordinates(Coordinates longlat) { try { HttpClient client = new HttpClient(); client.BaseAddress = ServerRootURL; client.DefaultRequestHeaders.Add("Accept", "application/json"); string requestJson = JsonConvert.SerializeObject(longlat); StringContent requestContent = new StringContent(requestJson); requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync(ServerLocation + "getlocation", requestContent); //If reponse is true if (response.IsSuccessStatusCode) { return "Saved on server"; } //If resource not found else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return "Resource " + response.ReasonPhrase; } else return "Unable to save"; } catch (System.Net.WebException ex) { return "Server Not Found"; } catch (Exception ex) { throw; } }
/// <summary> /// Retreiveing Data /// </summary> /// <param name="longlat"></param> /// <returns></returns> async public static Task<Coordinates> GetLastKnownLocation(Coordinates longlat) { try { RequestModel rm = new RequestModel(); rm.LocationsObject = longlat; HttpClient client = new HttpClient(); client.BaseAddress = ServerRootURL; client.DefaultRequestHeaders.Add("Accept", "application/json"); string requestJson = JsonConvert.SerializeObject(rm); StringContent requestContent = new StringContent(requestJson); requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync(ServerLocation + "tracelastlocation", requestContent); if (response.StatusCode == System.Net.HttpStatusCode.NoContent) { Toast.MakeText(Application.Context, "Id Not Available", ToastLength.Short).Show(); return null; } else if (response.IsSuccessStatusCode) { string lastLocation = await response.Content.ReadAsStringAsync(); longlat = JsonConvert.DeserializeObject<Coordinates>(lastLocation); return longlat; } //If resource not found else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return null; } //Id content not found else return null; } catch (System.Net.WebException ex) { return null; } catch (TaskCanceledException ex) { throw; } catch (Exception ex) { throw; } }
/// <summary> /// Street Address /// </summary> /// <param name="latlon"></param> async private void streetAddress(Coordinates latlon) { try { if (latlon == null || latlon.Latitude == 0 || latlon.Longitude == 0 ) { _streetAddress.Text = "Can't determine the current address."; return; } Geocoder geocoder = new Geocoder(this); IList<Address> addressList = await geocoder.GetFromLocationAsync(latlon.Latitude, latlon.Longitude, 10); Address address; List<string> allLocations = new List<string>(); for (int j = 0; j < addressList.Count; j++) { address = addressList.ElementAt(j); if (address != null) { StringBuilder deviceAddress = new StringBuilder(); for (int i = 0; i <= address.MaxAddressLineIndex; i++) { deviceAddress.Append(address.GetAddressLine(i)).AppendLine(","); } _streetAddress.Text = deviceAddress.ToString(); } else { _streetAddress.Text = "Unable to determine the address."; } allLocations.Add(_streetAddress.Text); } _streetAddress.Text = string.Join("\n", allLocations); } catch (Java.IO.IOException ex) { Toast.MakeText(this, "Network is unreachable", ToastLength.Short).Show(); } catch (Exception ex) { throw; } }