Example #1
0
        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
            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
                        builder.Include(new LatLng(_eventLocation.Latitude, _eventLocation.Longitude));
                        builder.Include(new LatLng(_currentUserLocation.Latitude, _currentUserLocation.Longitude));
                        LatLngBounds bounds = builder.Build();
                        cameraUpdate        = CameraUpdateFactory.NewLatLngBounds(bounds, 0);
                    }
                    // Set the map position
                    RunOnUiThread(() => { GMap.MoveCamera(cameraUpdate); });
                }

                // Add a markers
                RunOnUiThread(() => {
                    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();
                RunOnUiThread(() => { Map.OnEnterAmbient(null); });
            });

            // Set the map type back to normal.
            this.RunOnUiThread(() => { GMap.MapType = GoogleMap.MapTypeNormal; });
        }