首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用MKAnnotationView制作MKPointAnnotation链接

用MKAnnotationView制作MKPointAnnotation链接
EN

Stack Overflow用户
提问于 2016-03-19 05:21:10
回答 2查看 810关注 0票数 0

我一直在努力使用MapKit,我正在开发一款可以在地图上显示特定位置的应用程序。我已经设法通过使用坐标,标题和副标题的MKPointAnnotation代码添加引脚。现在,我想在右侧添加callout按钮,以转到一个屏幕,其中包含有关该地点的更多信息。我一直在尝试,但都没有结果。有人能帮上忙吗?

到目前为止我的代码是:

代码语言:javascript
复制
import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var map: MKMapView!

    var locationManager = CLLocationManager()

    class CustomPointAnnotation: MKPointAnnotation {
        var imageName: String!
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Request location

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

            self.map.showsUserLocation = true;

        // Map

        var latitude:CLLocationDegrees = 52.630886
        var longitude:CLLocationDegrees = 1.297355

        var latDelta:CLLocationDegrees = 0.05
        var lonDelta:CLLocationDegrees = 0.05

        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

        var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

        var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

        map.setRegion(region, animated: true)

        // Castle 52.628599, 1.296355

        var castle = MKPointAnnotation()
        castle.coordinate.latitude = 52.628599
        castle.coordinate.longitude = 1.296355
        castle.title = "Norwich Castle"
        castle.subtitle = "Subtitle..."
        map.addAnnotation(castle)

        // Cathedral 52.631869, 1.300919

        var cathedral = MKPointAnnotation()
        cathedral.coordinate.latitude = 52.631869
        cathedral.coordinate.longitude = 1.300919
        cathedral.title = "Norwich Cathedral"
        cathedral.subtitle = "Subtitle..."
        map.addAnnotation(cathedral)

        // The Guildhall 52.629032, 1.292522

        var guildhall = MKPointAnnotation()
        guildhall.coordinate.latitude = 52.629032
        guildhall.coordinate.longitude = 1.292522
        guildhall.title = "The Guildhall"
        guildhall.subtitle = "Subtitle..."
        map.addAnnotation(guildhall)

        // The Halls 52.631120, 1.295712

        var halls = MKPointAnnotation()
        halls.coordinate.latitude = 52.631120
        halls.coordinate.longitude = 1.295712
        halls.title = "The Halls"
        halls.subtitle = "Subtitle..."
        map.addAnnotation(halls)

        // The Assembly House 52.626867, 1.290733

        var assembly = MKPointAnnotation()
        assembly.coordinate.latitude = 52.626867
        assembly.coordinate.longitude = 1.290733
        assembly.title = "The Assembly House"
        assembly.subtitle = "Subtitle..."
        map.addAnnotation(assembly)

        // Surrey House 52.625162, 1.293919

        var surrey = MKPointAnnotation()
        surrey.coordinate.latitude = 52.625162
        surrey.coordinate.longitude = 1.293919
        surrey.title = "Surrey House"
        surrey.subtitle = "Subtitle..."
        map.addAnnotation(surrey)

        // City Hall 52.628645, 1.291606

        var cityhall = MKPointAnnotation()
        cityhall.coordinate.latitude = 52.628645
        cityhall.coordinate.longitude = 1.291606
        cityhall.title = "City Hall"
        cityhall.subtitle = "Subtitle..."
        map.addAnnotation(cityhall)
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println(locations)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

到目前为止还好吗?任何帮助将不胜感激,这是我的第一个应用程序,所以在简单的术语将是伟大的!

谢谢,

相同的

EN

回答 2

Stack Overflow用户

发布于 2016-03-19 05:49:57

您可以在MKPinAnnotationView实例中设置rightCalloutAccessoryView。

以下是objective c中的示例代码...

代码语言:javascript
复制
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *identifier = your_identifier;

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
    annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.image=[UIImage imageNamed:@"arrest.png"];//here we use a nice image instead of the default pins


    //set right CalloutAccessoryView
    annotationView.rightCalloutAccessoryView =  UIButton.buttonWithType(buttonType: UIButtonTypeDetailDisclosure)

    return annotationView;

    }
票数 2
EN

Stack Overflow用户

发布于 2016-03-19 05:46:37

您可能需要实现这些mapview委托方法

代码语言:javascript
复制
func mapView(_ mapView: MKMapView,
viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
  return annotationView
}

代码语言:javascript
复制
func mapView(_ mapView: MKMapView,
           annotationView view: MKAnnotationView,calloutAccessoryControlTapped control: UIControl)
{
     // your action 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36094442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档