ios-samples/WeatherMap/WeatherMapViewController.cs

129 строки
3.7 KiB
C#
Исходник Постоянная ссылка Обычный вид История

2015-01-19 18:09:40 +03:00
//
2011-08-27 02:33:22 +04:00
// WeatherMapViewController.cs
2015-01-19 18:09:40 +03:00
//
2011-08-27 02:33:22 +04:00
// Author: Jeffrey Stedfast <jeff@xamarin.com>
2015-01-19 18:09:40 +03:00
//
2011-08-27 02:33:22 +04:00
// Copyright (c) 2011 Xamarin Inc.
2015-01-19 18:09:40 +03:00
//
2011-08-27 02:33:22 +04:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2015-01-19 18:09:40 +03:00
//
2011-08-27 02:33:22 +04:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2015-01-19 18:09:40 +03:00
//
2011-08-27 02:33:22 +04:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
2015-01-19 18:09:40 +03:00
using CoreGraphics;
using UIKit;
using MapKit;
using Foundation;
using CoreLocation;
2011-08-27 02:33:22 +04:00
namespace WeatherMap
{
public partial class WeatherMapViewController : UIViewController
{
WeatherForecastAnnotation[] annotations;
WeatherServer weatherServer;
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
public WeatherMapViewController (string nibName, NSBundle bundle) : base (nibName, bundle)
{
weatherServer = new WeatherServer ();
Title = "Weather Map";
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
// Release any cached data, images, etc that aren't in use.
}
2015-01-19 18:09:40 +03:00
MKAnnotationView GetWeatherAnnotationView (MKMapView map, IMKAnnotation annotation)
2011-08-27 02:33:22 +04:00
{
MKAnnotationView annotationView = mapView.DequeueReusableAnnotation ("annotationViewID");
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
if (annotationView == null)
annotationView = new WeatherAnnotationView (annotation, "annotationViewID");
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
annotationView.Annotation = (MKAnnotation) annotation;
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
return annotationView;
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
// Get the region for North America
MKCoordinateRegion region = new MKCoordinateRegion (
new CLLocationCoordinate2D (37.37, -96.24),
new MKCoordinateSpan (28.49, 31.025)
);
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
// Connect to the RegionChanged event so we can update the displayed forecasts
// depending on what area of the map is shown.
mapView.RegionChanged += (sender, e) => {
// Remove the current annotations
if (annotations != null)
mapView.RemoveAnnotations (annotations);
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
// Get and set our new list of WeatherForecastAnnotations
annotations = weatherServer.GetForecastAnnotations (mapView.Region, 4);
2015-01-19 18:09:40 +03:00
mapView.AddAnnotations (annotations);
2011-08-27 02:33:22 +04:00
};
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
mapView.GetViewForAnnotation += GetWeatherAnnotationView;
mapView.SetRegion (region, false);
2011-08-27 02:33:22 +04:00
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
public override void ViewDidUnload ()
{
base.ViewDidUnload ();
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
// Release any retained subviews of the main view.
// e.g. myOutlet = null;
if (annotations != null) {
for (int i = 0; i < annotations.Length; i++) {
annotations[i].Dispose ();
annotations[i] = null;
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
annotations = null;
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
mapView.Dispose ();
mapView = null;
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return false;
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
protected override void Dispose (bool disposing)
{
if (weatherServer != null) {
weatherServer.Dispose ();
weatherServer = null;
}
2015-01-19 18:09:40 +03:00
2011-08-27 02:33:22 +04:00
base.Dispose (disposing);
}
}
}