正如 Headers 所述,我想通过按下按钮在我自己的应用程序中打开ios设备中的本机 Map 应用程序 . 目前我使用 MKmapview
显示一个简单的引脚,其中lat / long取自 json
文件 .
代码是这样的:
- (void)viewDidLoad {
[super viewDidLoad];
}
// We are delegate for map view
self.mapView.delegate = self;
// Set title
self.title = self.location.title;
// set texts...
self.placeLabel.text = self.location.place;
self.telephoneLabel.text = self.location.telephone;
self.urlLabel.text = self.location.url;
**// Make a map annotation for a pin from the longitude/latitude points
MapAnnotation *mapPoint = [[MapAnnotation alloc] init];
mapPoint.coordinate = CLLocationCoordinate2DMake([self.location.latitude doubleValue], [self.location.longitude doubleValue]);
mapPoint.title = self.location.title;**
// Add it to the map view
[self.mapView addAnnotation:mapPoint];
// Zoom to a region around the pin
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500);
[self.mapView setRegion:region];
}`
当您触摸图钉时,会出现一个带有 Headers 和信息按钮的信息框 .
这是代码:
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *view = nil;
static NSString *reuseIdentifier = @"MapAnnotation";
// Return a MKPinAnnotationView with a simple accessory button
view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
if(!view) {
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.canShowCallout = YES;
view.animatesDrop = YES;
}
return view;
}
当我点击上面信息框中的按钮时,我想制作一个方法,打开 Map 应用程序,其中包含从当前用户位置到上面mapPoint的路线 . 那可能吗?我也可以自定义这个按钮的外观吗? (我的意思是喜欢在按钮上放置一个不同的图像,使其看起来像“按我指示方向”) .
很抱歉,如果这是一个愚蠢的问题,但这是我的第一个ios应用程序,Obj-c对我来说是一种全新的语言 .
感谢所有的回复提前 .
3 回答
您初始化一个按钮并向按钮添加操作:
Objective-C Code
使用mapPoint是您要指向的位置 .
Swift3 or later
请注意,
saddr
,daddr
可以是位置名称或位置坐标 . 所以directionsURL
可以是:更多选项参数(如传输类型, Map 类型......)请查看here
您可以使用以下代码打开带有方向的 Map :(假设您的id <MKAnnotation>类具有名为“coordinate”的CLLocationCoordinate2D公共属性)
您也可以更改按钮,实际上您使用的是标准样式按钮:
但您可以使用图像或标签分配自定义按钮:
以下是在Apple Map 上显示路线的工作代码 . 它适用于您目的地的当前位置,您只需要通过目的地的纬度和长度 .
另外,请在这里与我分享,如果发现任何问题,或者我们需要找到另一种方法来完成它 .