Example #1
0
        /*
         *      private void SetMapLocation()
         *      {
         *          if (GMap == null) return;
         *
         *          var location = Map.Tag as LatLng;
         *          if (location == null && _userLocation == null) return;
         *
         *          // Calculate the map position and zoom/size
         *          CameraUpdate cameraUpdate = null;
         *          if (_userLocation == null)
         *              cameraUpdate = CameraUpdateFactory.NewLatLngZoom(location, Constants.DefaultResponseMapZoom);
         *          else if (location == null)
         *              cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_userLocation, Constants.DefaultResponseMapZoom);
         *          else
         *          {
         *              var latDelta = Math.Abs(location.Latitude - _userLocation.Latitude);
         *              var longDelta = Math.Abs(location.Longitude - _userLocation.Longitude);
         *              var minLat = Math.Min(location.Latitude, _userLocation.Latitude) - latDelta / 4;
         *              var maxLat = Math.Max(location.Latitude, _userLocation.Latitude) + latDelta / 4;
         *              var minLong = Math.Min(location.Longitude, _userLocation.Longitude) - longDelta / 4;
         *              var maxLong = Math.Max(location.Longitude, _userLocation.Longitude) + longDelta / 4;
         *
         *              LatLngBounds.Builder builder = new LatLngBounds.Builder();
         *              builder.Include(new LatLng(minLat, minLong));
         *              builder.Include(new LatLng(maxLat, maxLong));
         *              // shouldn't need to include these but we'll include them just in case
         *              builder.Include(new LatLng(location.Latitude, location.Longitude));
         *              builder.Include(new LatLng(_userLocation.Latitude, _userLocation.Longitude));
         *              LatLngBounds bounds = builder.Build();
         *              cameraUpdate = CameraUpdateFactory.NewLatLngBounds(bounds, 0);
         *          }
         *
         *          // Set the map position
         *          GMap.MoveCamera(cameraUpdate);
         *
         *          // Add a markers
         *          if (location != null)
         *          {
         *              var markerOptions = new MarkerOptions();
         *              markerOptions.SetPosition(location);
         *              if (_colorHue > -1)
         *              {
         *                  var bmDescriptor = BitmapDescriptorFactory.DefaultMarker(_colorHue);
         *                  markerOptions.SetIcon(bmDescriptor);
         *              }
         *              GMap.AddMarker(markerOptions);
         *          }
         *          if (_userLocation != null)
         *          {
         *              var markerOptions0 = new MarkerOptions();
         *              markerOptions0.SetPosition(_userLocation);
         *              markerOptions0.SetIcon(UserLocationIcon);
         *              markerOptions0.Anchor(0.5f, 0.5f);
         *              GMap.AddMarker(markerOptions0);
         *          }
         *
         *          // Set the map type back to normal.
         *          GMap.MapType = GoogleMap.MapTypeNormal;
         *      }
         */



        private async Task DrawMapAsync(bool moveMap = true)
        {
            if (GMap == null)
            {
                return;
            }

            if (_eventLocation == null && _currentUserLocation == null)
            {
                return;
            }

            // Set callback to detect when map has finished loading
            //          _activity.RunOnUiThread(() => { GMap.SetOnMapLoadedCallback(new MapLoadedCallback(Map.Id)); });

            await Task.Run(() =>
            {
                // Calculate the map position and zoom/size
                CameraUpdate cameraUpdate = null;
                if (moveMap)
                {
                    if (_currentUserLocation == null)
                    {
                        // Only event location available
                        cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_eventLocation, Constants.DefaultResponseMapZoom);
                    }
                    else if (_eventLocation == null)
                    {
                        // Only user location available
                        cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_currentUserLocation, Constants.DefaultResponseMapZoom);
                    }
                    else
                    {
                        // Both locations available
                        // get deltas between those locations
                        var latDelta  = Math.Abs(_eventLocation.Latitude - _currentUserLocation.Latitude);
                        var longDelta = Math.Abs(_eventLocation.Longitude - _currentUserLocation.Longitude);
                        // get the boundaries of the map
                        var minLat  = Math.Min(_eventLocation.Latitude, _currentUserLocation.Latitude) - latDelta / 4;
                        var maxLat  = Math.Max(_eventLocation.Latitude, _currentUserLocation.Latitude) + latDelta / 4;
                        var minLong = Math.Min(_eventLocation.Longitude, _currentUserLocation.Longitude) - longDelta / 4;
                        var maxLong = Math.Max(_eventLocation.Longitude, _currentUserLocation.Longitude) + longDelta / 4;

                        LatLngBounds.Builder builder = new LatLngBounds.Builder();
                        builder.Include(new LatLng(minLat, minLong));
                        builder.Include(new LatLng(maxLat, maxLong));
                        // shouldn't need to include these but we'll include them just in case
                        LatLngBounds bounds = builder.Build();
                        cameraUpdate        = CameraUpdateFactory.NewLatLngBounds(bounds, 0);
                    }
                    // Set the map position
                    _activity.RunOnUiThread(() => { GMap.MoveCamera(cameraUpdate); });
                }

                _activity.RunOnUiThread(() => {
                    // Add a markers
                    if (_eventLocation != null)
                    {
                        if (EventLocationMarker == null)
                        {
                            var markerOptions = new MarkerOptions();
                            markerOptions.SetPosition(_eventLocation);
                            if (_colorHue > -1)
                            {
                                var bmDescriptor = BitmapDescriptorFactory.DefaultMarker(_colorHue);
                                markerOptions.SetIcon(bmDescriptor);
                            }
                            EventLocationMarker = GMap.AddMarker(markerOptions);
                        }
                        else
                        {
                            var bmDescriptor = BitmapDescriptorFactory.DefaultMarker(_colorHue);
                            EventLocationMarker.SetIcon(bmDescriptor);
                            EventLocationMarker.Position = _eventLocation;
                        }
                    }
                    if (_currentUserLocation != null)
                    {
                        if (UserLocationMarker == null)
                        {
                            var markerOptions0 = new MarkerOptions();
                            markerOptions0.SetPosition(_currentUserLocation);
                            markerOptions0.SetIcon(UserLocationIcon);
                            markerOptions0.Anchor(0.5f, 0.5f);
                            UserLocationMarker = GMap.AddMarker(markerOptions0);
                        }
                        else
                        {
                            UserLocationMarker.Position = _currentUserLocation;
                        }
                    }

                    Map.OnResume();
                    //                  Map.OnEnterAmbient(null);
                });
            });

            // Set the map type back to normal.
            _activity.RunOnUiThread(() => { GMap.MapType = GoogleMap.MapTypeNormal; });
        }
        /*
         *      private void SetMapLocation()
         *      {
         *          if (GMap == null) return;
         *
         *          var location = Map.Tag as LatLng;
         *          if (location == null && _userLocation == null) return;
         *
         *          // Calculate the map position and zoom/size
         *          CameraUpdate cameraUpdate = null;
         *          if (_userLocation == null)
         *              cameraUpdate = CameraUpdateFactory.NewLatLngZoom(location, Constants.DefaultResponseMapZoom);
         *          else if (location == null)
         *              cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_userLocation, Constants.DefaultResponseMapZoom);
         *          else
         *          {
         *              var latDelta = Math.Abs(location.Latitude - _userLocation.Latitude);
         *              var longDelta = Math.Abs(location.Longitude - _userLocation.Longitude);
         *              var minLat = Math.Min(location.Latitude, _userLocation.Latitude) - latDelta / 4;
         *              var maxLat = Math.Max(location.Latitude, _userLocation.Latitude) + latDelta / 4;
         *              var minLong = Math.Min(location.Longitude, _userLocation.Longitude) - longDelta / 4;
         *              var maxLong = Math.Max(location.Longitude, _userLocation.Longitude) + longDelta / 4;
         *
         *              LatLngBounds.Builder builder = new LatLngBounds.Builder();
         *              builder.Include(new LatLng(minLat, minLong));
         *              builder.Include(new LatLng(maxLat, maxLong));
         *              // shouldn't need to include these but we'll include them just in case
         *              builder.Include(new LatLng(location.Latitude, location.Longitude));
         *              builder.Include(new LatLng(_userLocation.Latitude, _userLocation.Longitude));
         *              LatLngBounds bounds = builder.Build();
         *              cameraUpdate = CameraUpdateFactory.NewLatLngBounds(bounds, 0);
         *          }
         *
         *          // Set the map position
         *          GMap.MoveCamera(cameraUpdate);
         *
         *          // Add a markers
         *          if (location != null)
         *          {
         *              var markerOptions = new MarkerOptions();
         *              markerOptions.SetPosition(location);
         *              if (_colorHue > -1)
         *              {
         *                  var bmDescriptor = BitmapDescriptorFactory.DefaultMarker(_colorHue);
         *                  markerOptions.SetIcon(bmDescriptor);
         *              }
         *              GMap.AddMarker(markerOptions);
         *          }
         *          if (_userLocation != null)
         *          {
         *              var markerOptions0 = new MarkerOptions();
         *              markerOptions0.SetPosition(_userLocation);
         *              markerOptions0.SetIcon(UserLocationIcon);
         *              markerOptions0.Anchor(0.5f, 0.5f);
         *              GMap.AddMarker(markerOptions0);
         *          }
         *
         *          // Set the map type back to normal.
         *          GMap.MapType = GoogleMap.MapTypeNormal;
         *      }
         */



        private void DrawMap(bool moveMap = true)
        {
            if (GMap == null)
            {
                return;
            }

            if (_eventLocation == null && _userLocation == null)
            {
                return;
            }

            // Calculate the map position and zoom/size
            CameraUpdate cameraUpdate = null;

            if (moveMap)
            {
                if (_userLocation == null)
                {
                    cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_eventLocation, Constants.DefaultResponseMapZoom);
                }
                else if (_eventLocation == null)
                {
                    cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_userLocation, Constants.DefaultResponseMapZoom);
                }
                else
                {
                    _latDelta  = Math.Abs(_eventLocation.Latitude - _userLocation.Latitude);
                    _longDelta = Math.Abs(_eventLocation.Longitude - _userLocation.Longitude);
                    var minLat  = Math.Min(_eventLocation.Latitude, _userLocation.Latitude) - _latDelta / 4;
                    var maxLat  = Math.Max(_eventLocation.Latitude, _userLocation.Latitude) + _latDelta / 4;
                    var minLong = Math.Min(_eventLocation.Longitude, _userLocation.Longitude) - _longDelta / 4;
                    var maxLong = Math.Max(_eventLocation.Longitude, _userLocation.Longitude) + _longDelta / 4;

                    LatLngBounds.Builder builder = new LatLngBounds.Builder();
                    builder.Include(new LatLng(minLat, minLong));
                    builder.Include(new LatLng(maxLat, maxLong));
                    // shouldn't need to include these but we'll include them just in case
                    builder.Include(new LatLng(_eventLocation.Latitude, _eventLocation.Longitude));
                    builder.Include(new LatLng(_userLocation.Latitude, _userLocation.Longitude));
                    LatLngBounds bounds = builder.Build();
                    cameraUpdate = CameraUpdateFactory.NewLatLngBounds(bounds, 0);
                }
                // Set the map position
                GMap.MoveCamera(cameraUpdate);
            }

            // Add a markers
            if (_eventLocation != null)
            {
                if (_eventLocationMarker == null)
                {
                    var markerOptions = new MarkerOptions();
                    markerOptions.SetPosition(_eventLocation);
                    if (_colorHue > -1)
                    {
                        var bmDescriptor = BitmapDescriptorFactory.DefaultMarker(_colorHue);
                        markerOptions.SetIcon(bmDescriptor);
                    }
                    _eventLocationMarker = GMap.AddMarker(markerOptions);
                }
                else
                {
                    _eventLocationMarker.Position = _eventLocation;
                }
            }
            if (_userLocation != null)
            {
                if (_userLocationMarker == null)
                {
                    var markerOptions0 = new MarkerOptions();
                    markerOptions0.SetPosition(_userLocation);
                    markerOptions0.SetIcon(UserLocationIcon);
                    markerOptions0.Anchor(0.5f, 0.5f);
                    _userLocationMarker = GMap.AddMarker(markerOptions0);
                }
                else
                {
                    _userLocationMarker.Position = _userLocation;
                }
            }

            // Set the map type back to normal.
            GMap.MapType = GoogleMap.MapTypeNormal;
        }