【问题标题】:Set a location to the Map in a business info page在商家信息页面中为地图设置位置
【发布时间】:2014-02-04 18:31:10
【问题描述】:

我正在为一家餐厅设计一个应用程序(而且,主要是为了与 iOS 开发人员一起练习)。

对于这个餐厅信息页面(地址、电话号码等),我在底部页面添加了一个较小的地图来显示餐厅所在的位置,但是:

  • 如何制作这张小地图来显示餐厅位置(我知道坐标)和
  • 如何编写代码,如果用户点击地图,它会切换到具有相同坐标的 Apple Map 应用程序。

谢谢

【问题讨论】:

    标签: ios map location


    【解决方案1】:

    您必须在您的viewcontroller.h 文件中使用MKMapViewDelegate 并使用ma 委托的方法来处理地图。

    尝试使用下面的代码添加餐厅的图钉并将地图居中放置。

    MKAnnotationView *point = [[MKAnnotationView alloc] initWithAnnotation:_station reuseIdentifier:@"restaurant"];
    
    [map addAnnotation:point.annotation];
    
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.002;
    span.longitudeDelta = 0.002;
    region.span = span;
    region.center = location.coordinate;
    [map setRegion:region animated:YES];
    

    之后,执行didSelectAnnotationView 方法来识别按下的引脚。在其中,您可以使用餐厅坐标打开 Apple 地图:

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    
        NSString *title = @"title";
        float latitude = ...;
        float longitude = ...;
        int zoom = 13;
        NSString *stringURL = [NSString stringWithFormat:@"http://maps.apple.com?q=%@@%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
        NSURL *url = [NSURL URLWithString:stringURL];
        [[UIApplication sharedApplication] openURL:url];
    }
    

    编辑:

    我正在开发与您类似的应用程序,但在我的情况下是关于车站的。 MDStationController 是视图的控制器,其中包含顶部的地图(如您的)。 MDStation是车站。

    // MDStationController.h
    
    #import <UIKit/UIKit.h>
    #import "MDStation.h"
    #import <MapKit/MapKit.h>
    
    @interface MDStationController : UIViewController <MKMapViewDelegate>
    
    @property (nonatomic, retain) MDStation* station;
    
    @end
    
    // MDStationController.m 
    
    @implementation MDStationController
    @synthesize station=_station;
    
    [...]
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.navigationItem.title = _station.name;
        stationView.map.delegate = self;
        [self addAndcenterMapOnLocation:_station.location]; 
    }
    
    - (void) addAndcenterMapOnLocation:(CLLocation*)location {
    
        MKAnnotationView *point = [[MKAnnotationView alloc] initWithAnnotation:_station reuseIdentifier:@"station"];
    
        [stationView.map addAnnotation:point.annotation];
    
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta = 0.002;
        span.longitudeDelta = 0.002;
        region.span = span;
        region.center = location.coordinate;
        [stationView.map setRegion:region animated:YES];
    }
    
    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
        static NSString *defaultID = @"station";
        MKAnnotationView *pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultID];
        pinView.image = [UIImage imageNamed:@"bike_gray"];
        return pinView;
    }
    
    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    
        NSString *title = _station.name;
        float latitude = _station.location.latitude;
        float longitude = _station.location.longitude;
        int zoom = 13;
        NSString *stringURL = [NSString stringWithFormat:@"http://maps.apple.com?q=%@@%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
        NSURL *url = [NSURL URLWithString:stringURL];
        [[UIApplication sharedApplication] openURL:url];
    }
    

    【讨论】:

    • 谢谢!我会调查的!
    • 好的,我有 10 个编译器错误。我想我有什么问题。我在这个项目上也没有其他任何东西,所以也许这就是原因。你说要在 ViewController.h 文件中输入它,但我为联系人视图创建了一个 ContactVC.h&m 文件,这是正确的(我在书中学到的)谢谢
    • 您还有问题吗?
    • 您好!感谢这篇详细的帖子。不幸的是,我试图让它适应我的需要,而 Xcode 给了我几个(!)。所有关于它不知道的 .name、.location、_restaurantView.map 等......我想我没有以正确的方式实现或初始化它们......
    • 我是 Objective-C/iOS 的新手......从玩 Maps 开始对于我这个级别的人来说可能太难了。我试图按照书中的教程进行操作,但我发现自己无法处理...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2013-02-03
    • 2011-09-19
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多