Example #1
0
        async void getUserLocation()
        {
            try
            {
                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy = 50;

                var position = await locator.GetPositionAsync();

                if (position == null)
                {
                    return;
                }
                currentLat      = position.Latitude;
                currentLong     = position.Longitude;
                currentPosition = new Position(currentLat, currentLong);


                /*
                 * Debug.WriteLine("Position Latitude: {0}", position.Latitude);
                 * Debug.WriteLine("Position Longitude: {0}", position.Longitude);
                 * Debug.WriteLine("Position Latitude: {0}", currentLat);
                 * Debug.WriteLine("Position Longitude: {0}", currentLong);
                 */
                map = new Map(
                    MapSpan.FromCenterAndRadius(currentPosition, Distance.FromMiles(1)))
                {
                    IsShowingUser     = true,
                    VerticalOptions   = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };

                //building starting position field with user's current lat/long
                string text = currentLat + "," + currentLong;

                //sends request to google
                String url          = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + text + "&key=" + googleKey;
                var    jsonResponse = await SendRequest(url);

                if (!jsonResponse.IsSuccessStatusCode)
                {
                    //bad response, debu will output something and Starting position will be set to default which is "Starting Position..."
                    System.Diagnostics.Debug.WriteLine("Bad Coordinates?");
                }
                else
                {
                    string content = await jsonResponse.Content.ReadAsStringAsync();

                    System.Diagnostics.Debug.WriteLine(content);

                    //Parse JSON data to extract address
                    GeocodeResponse parsedResponse = JsonConvert.DeserializeObject <GeocodeResponse>(content);
                    myAddress = parsedResponse.Results[0].Formatted_address;
                }


                start = new SearchBar
                {
                    Placeholder             = myAddress,
                    HorizontalTextAlignment = TextAlignment.Start
                };

                start.Focused += startFocus;

                dest = new SearchBar
                {
                    Placeholder             = "Where are you going?",
                    HorizontalTextAlignment = TextAlignment.Start
                };

                Button submit = new Button
                {
                    Text            = "Submit Request",
                    BackgroundColor = Color.Tomato
                };
                submit.Clicked += onSubmitClicked;


                //When search bar is clicked
                dest.Focused += destFocus;

                var layout = new StackLayout
                {
                    VerticalOptions = LayoutOptions.FillAndExpand
                };

                layout.Children.Add(start);
                layout.Children.Add(dest);
                layout.Children.Add(map);
                layout.Children.Add(submit);
                Content = layout;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
            }
        }