using System;
using System.Linq;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.FormsEdition;
using Xamarin.Forms;
namespace GettingStarted
{
public partial class MainPage : ContentPage
{
private MapView mapView;
private Proj4Projection bingToWgs84Projection;
private Command goToFullExtentCommand;
private Command goToPreviousExtentCommand;
private Command goToNextExtentCommand;
private Command goToCurrentLocationCommand;
private Command goToAboutCommand;
private long previousLocationUpdatedTicks;
private ZoomLevelOptions zoomLevelOptions;
public MainPage()
{
InitializeComponent();
BindingContext = this;
bingToWgs84Projection = new Proj4Projection();
bingToWgs84Projection.InternalProjectionParametersString = ManagedProj4Projection.GetBingMapParametersString();
bingToWgs84Projection.ExternalProjectionParametersString = ManagedProj4Projection.GetWgs84ParametersString();
bingToWgs84Projection.Open();
mapView = new MapView();
mapView.MapUnit = GeographyUnit.Meter;
mapView.BackgroundColor = Color.FromRgb(244, 242, 238);
mapView.CurrentExtent = new RectangleShape(-13939426.6371, 6701997.4056, -7812401.86, 2626987.386962);
// Touch events
mapView.MapSingleTap += MapView_MapSingleTap;
mapView.MapLongPress += MapView_MapLongPress;
// Base overlay.
WorldMapKitOverlay worldOverlay = new WorldMapKitOverlay();
worldOverlay.Projection = WorldMapKitProjection.SphericalMercator;
mapView.Overlays.Add("WMK", worldOverlay);
// Location marker layer.
MarkerOverlay locationOverlay = new MarkerOverlay();
mapView.Overlays.Add("LocationOverlay", locationOverlay);
// Single tap popup overlay.
PopupOverlay popupOverlay = new PopupOverlay();
mapView.Overlays.Add("PopupOverlay", popupOverlay);
mapView.MapTools.CenterCoordinate.IsEnabled = true;
mapView.MapTools.CenterCoordinate.DisplayProjection = bingToWgs84Projection;
mapView.MapTools.CenterCoordinate.DisplayTextFormat = "Longitude:{0:N4}, Latitude:{1:N4}";
DependencyService.Get<IPlatformService>().RegisterLocationService(OnLocationUpdated);
Content = mapView;
}
public Command GoToFullExtentCommand
{
get
{
goToFullExtentCommand = (goToFullExtentCommand ?? new Command(GoToFullExtent));
return goToFullExtentCommand;
}
}
public Command GoToPreviousExtentCommand
{
get
{
goToPreviousExtentCommand = goToPreviousExtentCommand ?? new Command(GoToPreviousExtent);
return goToPreviousExtentCommand;
}
}
public Command GoToNextExtentCommand
{
get
{
goToNextExtentCommand = goToNextExtentCommand ?? new Command(GoToNextExtent);
return goToNextExtentCommand;
}
}
public Command GoToCurrentLocationCommand
{
get
{
goToCurrentLocationCommand = goToCurrentLocationCommand ?? new Command(GoToCurrentLocation);
return goToCurrentLocationCommand;
}
}
public Command GoToAboutCommand
{
get
{
goToAboutCommand = goToAboutCommand ?? new Command(GoToAbout);
return goToAboutCommand;
}
}
private void MapView_MapSingleTap(object sender, TouchMapViewEventArgs e)
{
PointShape worldPoint = e.PointInWorldCoordinate;
PointShape wgs84Point = (PointShape)bingToWgs84Projection.ConvertToExternalProjection(worldPoint);
string displayText = string.Format("Longitude : {0:N4}\r\nLatitude : {1:N4}", wgs84Point.X, wgs84Point.Y);
PopupOverlay popupOverlay = (PopupOverlay)mapView.Overlays["PopupOverlay"];
Popup popup = popupOverlay.Popups.FirstOrDefault() ?? new Popup();
popup.Position = worldPoint;
popup.Content = displayText;
popupOverlay.Popups.Clear();
popupOverlay.Popups.Add(popup);
popupOverlay.Refresh();
}
private async void MapView_MapLongPress(object sender, TouchMapViewEventArgs e)
{
zoomLevelOptions = zoomLevelOptions ?? new ZoomLevelOptions();
string action = await DisplayActionSheet("Zoom to scale?", "Cancel", null, zoomLevelOptions.Select(z => z.Name).ToArray());
ZoomLevel zoomLevel = zoomLevelOptions.FirstOrDefault(z => z.Name.Equals(action));
if (zoomLevel != null)
{
mapView.ZoomToScale(zoomLevel.Scale);
}
}
private void GoToFullExtent()
{
Overlay worldOverlay = mapView.Overlays["WMK"];
mapView.CurrentExtent = worldOverlay.GetBoundingBox();
mapView.Refresh();
}
private void GoToPreviousExtent()
{
mapView.ZoomToPreviousExtent();
}
private void GoToNextExtent()
{
mapView.ZoomToNextExtent();
}
private void GoToCurrentLocation()
{
MarkerOverlay locaterOverlay = (MarkerOverlay)mapView.Overlays["LocationOverlay"];
Marker locateMarker = locaterOverlay.Markers.FirstOrDefault();
if (locateMarker != null)
{
mapView.ZoomTo(locateMarker.Position, 18023);
}
}
private void GoToAbout()
{
Device.OpenUri(new Uri("http://thinkgeo.com/map-suite-developer-gis/ios-edition/"));
}
private void OnLocationUpdated(Point newLocation, float horizontalAccuracy)
{
long currentTicks = DateTime.Now.Ticks;
// Check the location update time, if is less that 5 seconds, don't need update the GPS marker.
if (TimeSpan.FromTicks(currentTicks - previousLocationUpdatedTicks).TotalSeconds > 5)// && !mapView.ExtentOverlay.IsBusy)
{
previousLocationUpdatedTicks = currentTicks;
}
else
{
return;
}
PointShape currentLocationInWgs84 = new PointShape(newLocation.X, newLocation.Y);
PointShape currentLocationInMercator = (PointShape)bingToWgs84Projection.ConvertToInternalProjection(currentLocationInWgs84);
MarkerOverlay locationOverlay = (MarkerOverlay)mapView.Overlays["LocationOverlay"];
GpsMarker locationMarker = locationOverlay.Markers.OfType<GpsMarker>().FirstOrDefault();
// Create a location marker if not exists.
if (locationMarker == null)
{
locationMarker = new GpsMarker();
locationOverlay.Markers.Add(locationMarker);
}
// Update the GPS marker position.
locationMarker.HaloRadiusInMeter = horizontalAccuracy;
locationMarker.Position = currentLocationInMercator;
locationOverlay.Refresh();
}
}
}
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly:|AssemblyTitle("GettingStarted")]
[assembly: AssemblyDescription("")]
[assembly:|AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly:|AssemblyProduct("GettingStarted")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly:|AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly:|NeutralResourcesLanguage("en")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly:|AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly:|AssemblyFileVersion("1.0.0.0")]