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); }); }); }
/// <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)); } }
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); } }
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() { // 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; } }