public async Task GetPositionFromKnownAddressTest()
        {
            string   addressString = "3 Russel Street, GlandStone, Invercargill, New Zealand";
            Position position      = await GoogleMapsAPI.GetPositionFromKnownAddress(addressString);

            Assert.AreEqual(-46.3, position.Latitude, 0.1);
            //addressString = "140 Main Street, Gore, New Zealand";
            //position = await GoogleMapsAPI.GetPositionFromKnownAddress(addressString);
            //Assert.AreEqual(-46.1, position.Latitude, 0.1);
            addressString = "Invercargill, Russel st 3 ";
            position      = await GoogleMapsAPI.GetPositionFromKnownAddress(addressString);

            Assert.AreEqual(0, position.Latitude, 0.5);
        }
        private async void CheckEndPoint()
        {
            if (EndPoint == Preferences.Get("EndPoint", ""))
            {
                return;
            }
            Position position = await GoogleMapsAPI.GetPositionFromKnownAddress(_endPoint);

            bool isSuccess = !(position.Latitude == 0 && position.Longitude == 0);

            if (isSuccess)
            {
                Preferences.Set("EndPoint", EndPoint);
                Preferences.Set("EndPointGeoWaypoint", $"{position.Latitude}%2C{position.Longitude}");
                GoogleMapsAPI.DestinationAddress  = EndPoint;
                GoogleMapsAPI.DestinationPosition = position;
            }

            Validate(() => !isSuccess, "Set the End Point as " + _endPoint, "Message");
            Validate(() => isSuccess, "The address is not valid, Please try again", "Message"); // Message is not a real property name
        }
Example #3
0
        //initialize markers on map and position into _waypoints
        private async Task <bool> InitPins()
        {
            _pins.Clear();
            map.Pins.Clear();
            _waypoints.Clear();

            for (int i = 0; i < _invoiceSQLite.Count; i++)
            {
                ContactSQLite customerContact = App.ContactDatabase.GetContactByID(_invoiceSQLite[i].ContactID);
                Position      position;

                if (!customerContact.Latitude.HasValue)
                {
                    //Get better format from address
                    #region Format the Address
                    string fullAddress = customerContact.Address;
                    if (fullAddress == "")
                    {
                        continue;
                    }
                    else if (customerContact.City != "")
                    {
                        fullAddress += $", {customerContact.City}";
                    }
                    fullAddress += $", New Zealand";
                    #endregion

                    if (App.CheckIfInternet())
                    {
                        //Get location by calling google geolocation API / each invoice
                        position = await GoogleMapsAPI.GetPositionFromKnownAddress(fullAddress);

                        customerContact.Latitude  = position.Latitude;
                        customerContact.Longitude = position.Longitude;
                    }
                    else
                    {
                        try {
                            Location location = Geocoding.GetLocationsAsync(fullAddress).Result.FirstOrDefault();
                            position = new Position(location.Latitude, location.Longitude);
                            customerContact.Latitude  = position.Latitude;
                            customerContact.Longitude = position.Longitude;
                        }
                        catch
                        {
                            position = new Position(0, 0);
                            await DisplayAlert("Alert", String.Format("Couldnt map the position of {0}", _invoiceSQLite[i].InvoiceNumber), "OK");
                        }
                    }

                    if (customerContact.Latitude.HasValue)
                    {
                        App.ContactDatabase.UpdateContactPosition(customerContact);
                    }
                }
                else
                {
                    position = new Position(customerContact.Latitude.Value, customerContact.Longitude.Value);
                }

                if (position.Latitude != 0 && position.Longitude != 0)
                {
                    //Add it to the waypoints list later to be used on the googleDirection API
                    //Formatted by Comma separator for latitude,longitude
                    _waypoints.Add($"{position.Latitude}%2C{position.Longitude}");

                    //Set pin on map
                    #region SetPin
                    var pin = new Pin()
                    {
                        Position = position,
                        Label    = $"{_invoiceSQLite[i].InvoiceNumber}",
                        //set tag so i can reference it when a pin is clicked
                        Tag  = _invoiceSQLite[i].InvoiceID,
                        Icon = BitmapDescriptorFactory.FromBundle(_invoiceSQLite[i].InvoiceType),
                    };
                    pin.Address = "Click for Details";
                    _pins.Add(pin);

                    map.SelectedPinChanged += Map_SelectedPinChanged;
                    map.InfoWindowClicked  += Map_InfoWindowClicked;
                    map.Pins.Add(pin);
                    #endregion

                    _invoices.Add(_storageInvoice[i]);
                }
            }
            return(true);
        }