我有一个问题,当用户单击标注时,CalloutAccessoryControlTapped 永远不会被调用。这在 Apple 地图中是否已删除或更改?
在 iOS 5 中,我的调用如下所示:
(图片) 餐厅名称/地址 >
当我点击呼叫时,我会得到一个对话框。在 iOS6 中,我的调用如下所示:
餐厅名称/地址
为什么我在 Apple 地图中的呼叫发生了变化?
这是它在 iOS 6 中的样子:
这是我的代码:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MapKit;
using MonoTouch.CoreLocation;
using System.Linq;
using MenuFinderAN.BusinessLogic.MenuFinderServiceReference;
using System.Collections.Generic;
namespace MenuFinderMT
{
public partial class MapUniversalController : UIViewController
{
static bool UserInterfaceIdiomIsPhone {
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
}
public AppDelegate AppDelegate { get; set; }
public List<IGrouping<string, RestaurantLocation>> LocationList { get; set; }
public MapUniversalController (List<IGrouping<string, RestaurantLocation>> locationList)
: base (UserInterfaceIdiomIsPhone ? "MapUniversalController_iPhone" : "MapUniversalController_iPad", null)
{
LocationList = locationList;
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
MKMapView mapView;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
AppDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
Title = "Locations";
mapView = new MKMapView (View.Bounds);
mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
//mapView.MapType = MKMapType.Standard; // this is the default
//mapView.MapType = MKMapType.Satellite;
//mapView.MapType = MKMapType.Hybrid;
View.AddSubview (mapView);
List<RestaurantLocation> locations = LocationList.SelectMany (x => x).ToList ();
// create our location and zoom
double lat = 0;
double lng = 0;
foreach (RestaurantLocation loc in locations) {
if (loc.Latitude > 0 && lat == 0) {
lat = loc.Latitude;
lng = loc.Longitude;
}
// add a basic annotation
var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (loc.Latitude, loc.Longitude), loc.Name, loc.Address + ", " + loc.City);
mapView.AddAnnotation (annotation);
}
CLLocationCoordinate2D coords = new CLLocationCoordinate2D (lat, lng);
MKCoordinateSpan span = new MKCoordinateSpan (MilesToLatitudeDegrees (20), MilesToLongitudeDegrees (20, coords.Latitude));
// set the coords and zoom on the map
mapView.Region = new MKCoordinateRegion (coords, span);
// assign the delegate, which handles annotation layout and clicking
mapView.Delegate = new MapDelegate (this, locations);
}
/// <summary>
/// Converts miles to latitude degrees
/// </summary>
public double MilesToLatitudeDegrees (double miles)
{
double earthRadius = 3960.0;
double radiansToDegrees = 180.0 / Math.PI;
return (miles / earthRadius) * radiansToDegrees;
}
/// <summary>
/// Converts miles to longitudinal degrees at a specified latitude
/// </summary>
public double MilesToLongitudeDegrees (double miles, double atLatitude)
{
double earthRadius = 3960.0;
double degreesToRadians = Math.PI / 180.0;
double radiansToDegrees = 180.0 / Math.PI;
// derive the earth's radius at that point in latitude
double radiusAtLatitude = earthRadius * Math.Cos (atLatitude * degreesToRadians);
return (miles / radiusAtLatitude) * radiansToDegrees;
}
public override void ViewDidUnload ()
{
base.ViewDidUnload ();
// Clear any references to subviews of the main view in order to
// allow the Garbage Collector to collect them sooner.
//
// e.g. myOutlet.Dispose (); myOutlet = null;
ReleaseDesignerOutlets ();
}
}
// The map delegate is much like the table delegate.
class MapDelegate : MKMapViewDelegate
{
protected string annotationIdentifier = "BasicAnnotation";
MapUniversalController mapUniversalController;
List<RestaurantLocation> locations;
public MapDelegate (MapUniversalController mapUniversalController, List<RestaurantLocation> locations)
{
this.mapUniversalController = mapUniversalController;
this.locations = locations;
}
/// <summary>
/// This is very much like the GetCell method on the table delegate
/// </summary>
public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
{
// try and dequeue the annotation view
MKAnnotationView annotationView = mapView.DequeueReusableAnnotation (annotationIdentifier);
// if we couldn't dequeue one, create a new one
if (annotationView == null)
annotationView = new MKPinAnnotationView (annotation, annotationIdentifier);
else // if we did dequeue one for reuse, assign the annotation to it
annotationView.Annotation = annotation;
// configure our annotation view properties
annotationView.CanShowCallout = true;
(annotationView as MKPinAnnotationView).AnimatesDrop = true;
(annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Green;
annotationView.Selected = true;
// you can add an accessory view, in this case, we'll add a button on the right, and an image on the left
annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
annotationView.LeftCalloutAccessoryView = new UIImageView (UIImage.FromBundle ("Images/Icons/MenuFinder_Logo-29x29.png"));
return annotationView;
}
public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control)
{
var annotation = view.Annotation;
var coords = (annotation as MKAnnotation).Coordinate;
RestaurantLocation restaurantLocation = locations.Where (a => a.Latitude == coords.Latitude && a.Longitude == coords.Longitude).FirstOrDefault ();
UIAlertView alertt = new UIAlertView ("Directions?", "Would you like directions?", null, "Yes", null);
alertt.AddButton ("No");
alertt.Clicked += delegate(object sender, UIButtonEventArgs e) {
if (e.ButtonIndex == 0) {
double slat = (double)mapUniversalController.AppDelegate.Latitude;
double slng = (double)mapUniversalController.AppDelegate.Longitude;
string saddr = slat.ToString () + "," + slng.ToString ();
string daddr = coords.Latitude.ToString () + "," + coords.Longitude.ToString ();
NSUrl url = new NSUrl ("http://maps.google.com/maps?saddr=" + saddr + "&daddr=" + daddr);
UIApplication.SharedApplication.OpenUrl (url);
} else {
mapUniversalController.AppDelegate.CurrentRestaurantLocation = restaurantLocation;
// Pass the selected object to the new view controller.
mapUniversalController.NavigationController.PushViewController (new RestaurantDetailsUniversalController (), true);
}
};
alertt.Show ();
}
// as an optimization, you should override this method to add or remove annotations as the
// map zooms in or out.
public override void RegionChanged (MKMapView mapView, bool animated)
{
}
}
class BasicMapAnnotation : MKAnnotation
{
/// <summary>
/// The location of the annotation
/// </summary>
public override CLLocationCoordinate2D Coordinate { get; set; }
protected string title;
protected string subtitle;
/// <summary>
/// The title text
/// </summary>
public override string Title
{ get { return title; } }
/// <summary>
/// The subtitle text
/// </summary>
public override string Subtitle
{ get { return subtitle; } }
public BasicMapAnnotation (CLLocationCoordinate2D coordinate, string title, string subTitle)
: base()
{
this.Coordinate = coordinate;
this.title = title;
this.subtitle = subTitle;
}
}
}