public void ShowAustralia (View v) { // Wait until map is ready if (map == null) return; // Create bounds that include all locations of the map var boundsBuilder = new LatLngBounds.Builder () .Include(PERTH) .Include(ADELAIDE) .Include(MELBOURNE) .Include(SYDNEY) .Include(DARWIN) .Include(BRISBANE); // Move camera to show all markers and locations map.MoveCamera (CameraUpdateFactory.NewLatLngBounds (boundsBuilder.Build (), 20)); }
private void ZoomAndCenterMap(IEnumerable<HeritageProperty> items) { Task.Run(async () => { // wait a bit await Task.Delay(50); // invoke on main thread Handler.InvokeOnMainThread(() => { // create a bounds builder LatLngBounds.Builder builder = new LatLngBounds.Builder(); // loop all the properties and add them as markers foreach (var item in items) builder.Include(new LatLng(item.Latitude, item.Longitude)); // zoom the map in CameraUpdate cu = CameraUpdateFactory.NewLatLngBounds(builder.Build(), 100); this.NativeMap.MoveCamera(cu); this.NativeMap.AnimateCamera(cu); }); }); }
private void SetUpMap() { // Hide the zoom controls as the button panel will cover it. mMap.UiSettings.ZoomControlsEnabled = false; // Add lots of markers to the map. AddMarkersToMap(); // Setting an info window adapter allows us to change the both the contents and look of the // info window. mMap.SetInfoWindowAdapter (new CustomInfoWindowAdapter (this)); // Set listeners for marker events. See the bottom of this class for their behavior. mMap.SetOnMarkerClickListener(this); mMap.SetOnInfoWindowClickListener(this); mMap.SetOnMarkerDragListener(this); // Pan to see all markers in view. // Cannot zoom to bounds until the map has a size. View mapView = SupportFragmentManager.FindFragmentById (Resource.Id.map).View; if (mapView.ViewTreeObserver.IsAlive) { GlobalLayoutListener gll = null; gll = new GlobalLayoutListener ( delegate { LatLngBounds bounds = new LatLngBounds.Builder() .Include(PERTH) .Include(SYDNEY) .Include(ADELAIDE) .Include(BRISBANE) .Include(MELBOURNE) .Build (); /*if (Build.VERSION.SdkInt < Build.VERSION_CODES.JellyBean)*/ { mapView.ViewTreeObserver.RemoveGlobalOnLayoutListener (gll); } /* else { mapView.ViewTreeObserver.RemoveOnGlobalLayoutListener (this); }*/ mMap.MoveCamera (CameraUpdateFactory.NewLatLngBounds (bounds, 50)); }); mapView.ViewTreeObserver.AddOnGlobalLayoutListener (gll); } }
/// <summary> /// Updates location pins on the map. /// </summary> async void UpdateLocations () { if (googleMap == null) return; googleMap.Clear (); try { MapsInitializer.Initialize (this); } catch (GooglePlayServicesNotAvailableException e) { Console.WriteLine ("Google Play Services not available:" + e); return; } await assignmentViewModel.LoadAssignmentsAsync (); var points = new LatLngBounds.Builder (); foreach (var assignment in assignmentViewModel.Assignments) { var markerOptions = GetMarkerOptionsForAssignment (assignment); googleMap.AddMarker (markerOptions); points.Include (markerOptions.Position); } if (assignmentViewModel.ActiveAssignment != null) { var markerOptions = GetMarkerOptionsForAssignment (assignmentViewModel.ActiveAssignment); googleMap.AddMarker (markerOptions).ShowInfoWindow (); points.Include (markerOptions.Position); googleMap.CameraPosition.Target = markerOptions.Position; } var bounds = points.Build (); if (mapView.Width == 0) { initTry = 0; PostDelayInitMap (bounds); } else { googleMap.MoveCamera (CameraUpdateFactory.NewLatLngBounds (bounds, 0)); } }
public async void SetupMapIfNeeded() { if (_map == null && _mapFragment != null) { _map = _mapFragment.Map; } if (_map != null) { LatLng myLocation = new LatLng(_gpsTracker.Latitude, _gpsTracker.Longitude); _crueltySpotsService = new CrueltySpotsService(); _map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(myLocation, 11)); List<String> categories = UserPreferencesHelper.GetFilterCategories(); DebugHelper.WriteDebugEntry("Fetched categories for mapping (" + categories.Count + ")"); if (!categories.Any()) { _crueltySpots = await _crueltySpotsService.GetManyAsync(new CrueltySpot(), true, CrueltySpotSortField.CreatedAt, SortDirection.Asc); } else { _crueltySpots = await _crueltySpotsService.GetManyAsync(categories, true, CrueltySpotSortField.CreatedAt, SortDirection.Asc); } DebugHelper.WriteDebugEntry("Fetched CrueltySPots for mapping"); RunOnUiThread(() => { MarkerOptions mapOption; LatLng crueltyLocation; LatLngBounds.Builder builder = new LatLngBounds.Builder(); builder.Include(myLocation); foreach (CrueltySpot spot in _crueltySpots) { // Loop through List with foreach if (spot.Name.CompareTo("Universoul Circus") == 0) { continue; } crueltyLocation = new LatLng(spot.Latitude, spot.Longitude); builder.Include(crueltyLocation); float defaultValue = 33; if (_pinColorLookup.ContainsKey(spot.CrueltySpotCategory.IconName.Replace(".png", "pin"))) { defaultValue = _pinColorLookup[spot.CrueltySpotCategory.IconName.Replace(".png", "pin")]; } mapOption = new MarkerOptions() .SetPosition(crueltyLocation) .SetSnippet(spot.Address) .SetTitle(spot.Name) .InvokeIcon(BitmapDescriptorFactory.DefaultMarker(defaultValue)); Marker marker = _map.AddMarker(mapOption); _crueltyLookup.Add(marker.Id, spot); } // Move the map so that it is showing the markers we added above. _map.SetInfoWindowAdapter(new CustomInfoWindowAdapter(this)); // Set listeners for marker events. See the bottom of this class for their behavior. _map.SetOnInfoWindowClickListener(this); }); } }
void DrawRunTrack(bool mapShouldFollow) { if (mRunLocations.Count < 1) return; // Set up overlay for the map with the current run's locations // Create a polyline with all of the points PolylineOptions line = new PolylineOptions(); // Create a LatLngBounds so you can zoom to fit LatLngBounds.Builder latLngBuilder = new LatLngBounds.Builder(); // Add the locations of the current run foreach (RunLocation loc in mRunLocations) { LatLng latLng = new LatLng(loc.Latitude, loc.Longitude); line.Add(latLng); latLngBuilder.Include(latLng); } // Add the polyline to the map mGoogleMap.AddPolyline(line); // Add markers LatLng startLatLng = new LatLng(mRunLocations[0].Latitude, mRunLocations[0].Longitude); MarkerOptions startMarkerOptions = new MarkerOptions(); startMarkerOptions.SetPosition(startLatLng); startMarkerOptions.SetTitle(Activity.GetString(Resource.String.run_start)); startMarkerOptions.SetSnippet(Activity.GetString(Resource.String.run_started_at_format, new Java.Lang.Object[]{new Java.Lang.String(mRunLocations[0].Time.ToLocalTime().ToLongTimeString())})); mGoogleMap.AddMarker(startMarkerOptions); var activeRun = mRunManager.GetActiveRun(); if (activeRun == null || (activeRun != null && CurrentRun.Id != activeRun.Id)) { LatLng stopLatLng = new LatLng(mRunLocations[mRunLocations.Count-1].Latitude, mRunLocations[mRunLocations.Count-1].Longitude); MarkerOptions stopMarkerOptions = new MarkerOptions(); stopMarkerOptions.SetPosition(stopLatLng); stopMarkerOptions.SetTitle(Activity.GetString(Resource.String.run_finish)); stopMarkerOptions.SetSnippet(Activity.GetString(Resource.String.run_finished_at_format, new Java.Lang.Object[]{new Java.Lang.String(mRunLocations[mRunLocations.Count-1].Time.ToLocalTime().ToLongTimeString())})); mGoogleMap.AddMarker(stopMarkerOptions); } // Make the map zoom to show the track, with some padding // Use the size of the map linear layout in pixels as a bounding box LatLngBounds latLngBounds = latLngBuilder.Build(); // Construct a movement instruction for the map CameraUpdate movement = CameraUpdateFactory.NewLatLngBounds(latLngBounds, mMapWidth, mMapHeight, 50); if (mMapShouldFollow) { try { mGoogleMap.MoveCamera(movement); mMapShouldFollow = mapShouldFollow; } catch (Exception ex) { Console.WriteLine("[{0}] No Layout yet {1}", TAG, ex.Message); } } }
/// <summary> /// Fields the selected. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">E event.</param> public void FieldSelected(object sender, FieldSelectedEventArgs e) { if (this.Map != null) { var builder = new LatLngBounds.Builder(); var whc = GeoHelper.CalculateBoundingBox(e.Field.BoundingCoordinates); double width = whc.Width * 0.95; double height = whc.Height * 0.59; builder.Include(new LatLng(whc.Center.Latitude - width, whc.Center.Longitude - height)); builder.Include(new LatLng(whc.Center.Latitude + width, whc.Center.Longitude + height)); var bounds = builder.Build(); this.Map.MoveCamera(CameraUpdateFactory.NewLatLngBounds(bounds, 0)); } }
void ZoomToMyLocationAndAlarms () { var location = MyCurrentLocation; if (_mapData.Count > 0) { var boundsBuilder = new LatLngBounds.Builder (); foreach (var alarm in _mapData) { boundsBuilder.Include (new LatLng (alarm.Latitude, alarm.Longitude)); } if (location != null) { boundsBuilder.Include (new LatLng (location.Latitude, location.Longitude)); } try { _map.AnimateCamera (CameraUpdateFactory.NewLatLngBounds (boundsBuilder.Build (), 200)); Log.Debug (TAG, "map zoomed with NewLatLngBounds"); } catch { Log.Debug (TAG, "exception while zooming with NewLatLngBounds"); } } else { AnimateTo (location); } }
private void ZoomAndCenterMap() { // update the camera but give some time for the map to show Task.Run(async () => { await Task.Delay(100); RunOnUiThread(() => { // create a bounds builder LatLngBounds.Builder builder = new LatLngBounds.Builder(); // loop all the properties and add them as markers foreach (var item in this.Properties) builder.Include(new LatLng(item.Latitude, item.Longitude)); // zoom the map in CameraUpdate cu = CameraUpdateFactory.NewLatLngBounds(builder.Build(), 100); map.Map.MoveCamera(cu); map.Map.AnimateCamera(cu); }); }); }
void UpdateMap (int position) { if (googleMap == null) return; try { MapsInitializer.Initialize (this); } catch (GooglePlayServicesNotAvailableException e) { Console.WriteLine ("Google Play Services not available:" + e); error = true; #if !DEBUG Xamarin.Insights.Report (e, "GPS", "Not Available"); #endif return; } if (markerOptions == null) markerOptions = new MarkerOptions (); if (circleOptions == null) circleOptions = new CircleOptions (); var place = viewModel.Places [position]; var markerLatLong = new LatLng (place.Geometry.Location.Latitude, place.Geometry.Location.Longitude); if (marker == null) { markerOptions.SetTitle (place.Name); markerOptions.SetPosition (markerLatLong); marker = googleMap.AddMarker (markerOptions); } else { marker.Position = markerLatLong; marker.Title = place.Name; } var userLocation = new LatLng (viewModel.Position.Latitude, viewModel.Position.Longitude); if (locationCircle == null) { circleOptions.InvokeCenter (userLocation); circleOptions.InvokeRadius (radius); circleOptions.InvokeStrokeWidth (0); circleOptions.InvokeFillColor (Resources.GetColor (Resource.Color.accent)); locationCircle = googleMap.AddCircle (circleOptions); } else { locationCircle.Center = userLocation; locationCircle.Radius = radius; } if (!initMap) { var farthest = viewModel.Places [viewModel.Places.Count - 1]; var distance = farthest.GetDistance (userLocation.Latitude, userLocation.Longitude); radius = 20.0 * (distance / .45); radius = Math.Min (radius, 40.0); locationCircle.Radius = radius; var points = new LatLngBounds.Builder (); points.Include (userLocation); foreach (var p in viewModel.Places) points.Include (new LatLng (p.Geometry.Location.Latitude, p.Geometry.Location.Longitude)); var bounds = points.Build (); if (mapView.Width == 0) { initTry = 0; PostDelayInitMap (bounds); } else { googleMap.MoveCamera (CameraUpdateFactory.NewLatLngBounds (bounds, 0)); } initMap = true; } }