private void PrepareMap(StudentLocationModel slm) { try { if (mapview.Annotations != null) { mapview.RemoveAnnotations(mapview.Annotations); } //foreach (var item in mapview.Annotations) //{ // mapview.RemoveAnnotation(item); //} mapview.SetNeedsDisplay(); CLLocationCoordinate2D coordsforStudent = new CLLocationCoordinate2D(slm.student_lat, slm.student_lng); MKCoordinateSpan span1 = new MKCoordinateSpan(MilesToLatitudeDegrees(2), MilesToLongitudeDegrees(2, coordsforStudent.Latitude)); mapview.Region = new MKCoordinateRegion(coordsforStudent, span1); CLLocationCoordinate2D coordsforSource = new CLLocationCoordinate2D(slm.source_lat, slm.source_lng); MKCoordinateSpan span2 = new MKCoordinateSpan(MilesToLatitudeDegrees(2), MilesToLongitudeDegrees(2, coordsforSource.Latitude)); mapview.Region = new MKCoordinateRegion(coordsforSource, span2); CLLocationCoordinate2D coordsfordestinatin = new CLLocationCoordinate2D(slm.destination_lat, slm.destination_lng); MKCoordinateSpan span3 = new MKCoordinateSpan(MilesToLatitudeDegrees(2), MilesToLongitudeDegrees(2, coordsfordestinatin.Latitude)); mapview.Region = new MKCoordinateRegion(coordsfordestinatin, span3); // set the map delegate mapDel = new MyMapDelegate(); mapview.Delegate = mapDel; // add a custom annotation mapview.AddAnnotation(new CustomAnnotation(0, StaticDataModel.StudentInfo + "'s Home", coordsforStudent)); mapview.AddAnnotation(new CustomAnnotation(1, "Source", coordsforSource)); mapview.AddAnnotation(new CustomAnnotation(2, "Destination", coordsfordestinatin)); //// add a custom annotationTesting prupose //CLLocationCoordinate2D coords1 = new CLLocationCoordinate2D(StaticDataModel.Lattitude, StaticDataModel.Longitude); //mapview.AddAnnotation(new CustomAnnotation("Bus",coords1)); } catch (Exception ex) { } }
public static StudentLocationModel GetLatLongForStudentTracking(int student_id) { StudentLocationModel list = new StudentLocationModel(); try { var request = HttpWebRequest.Create(string.Format(BaseUrl + "student_lat_lng?student_id=" + student_id)); request.ContentType = "application/json"; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode); } using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var content = reader.ReadToEnd(); string Parsing = content.ToString(); var json = JsonValue.Parse(Parsing); JObject item = JObject.Parse(Parsing); list.student_lat = Convert.ToDouble(item["student_lat"].ToString()); list.student_lng = Convert.ToDouble(item["student_lng"].ToString()); list.source_lat = Convert.ToDouble(item["source_lat"].ToString()); list.source_lng = Convert.ToDouble(item["source_lng"].ToString()); list.destination_lat = Convert.ToDouble(item["destination_lat"].ToString()); list.destination_lng = Convert.ToDouble(item["destination_lng"].ToString()); } } return(list); } catch (Exception ex) { Console.WriteLine(ex.Message); return(list); } }
private void GetStudentLatLong() { model = new StudentLocationModel(); try { //BTProgressHUD.Show(); Task.Factory.StartNew( // tasks allow you to use the lambda syntax to pass wor () => { model = WebService.GetLatLongForStudentTracking(StaticDataModel.StudentId); } /// ).ContinueWith( t => { if (model != null) { InvokeOnMainThread(() => { PrepareMap(model); }); } else { } //BTProgressHUD.Dismiss(); }, TaskScheduler.FromCurrentSynchronizationContext() ); } catch (Exception ex) { } finally { } }