Objective c mkmapview问题上的多段线和覆盖
我遇到了这个问题,我的地图视图上的多段线和覆盖。也许其他人也有这样的问题?我无法使多段线工作,但其他一切都在工作。跟踪用户等,但只需将多段线添加到地图中即可 这是我的密码 h-fileObjective c mkmapview问题上的多段线和覆盖,objective-c,mkmapview,xcode6,mkpolyline,Objective C,Mkmapview,Xcode6,Mkpolyline,我遇到了这个问题,我的地图视图上的多段线和覆盖。也许其他人也有这样的问题?我无法使多段线工作,但其他一切都在工作。跟踪用户等,但只需将多段线添加到地图中即可 这是我的密码 h-file #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @class Walk; @interface DetailViewController : UI
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@class Walk;
@interface DetailViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) Walk *walk; //walk
@property (strong, nonatomic) IBOutlet MKMapView *MKMapView; //mapview
@property (strong, nonatomic) CLLocationManager *locationManager; //locationmanager
@property (strong, nonatomic) IBOutlet UIProgressView *myprogressView; //progressbar
@property (strong, nonatomic) IBOutlet UILabel *distLabel; //distancelabel
@property (strong, nonatomic) NSMutableArray *trackPointArray; //trackpointarray
@property (strong, nonatomic) CLLocationManager *locationCoordinates; //locationcoordinates
@property (nonatomic, assign) CLLocationDegrees *currentLocation; //currentlocation
@property (strong, nonatomic) UIActionSheet *saveWalk;
@end
#导入
#进口
#进口
@班级步行;
@界面详细信息ViewController:UIViewController
@属性(强,非原子)Walk*Walk//步行
@属性(强,非原子)IBMOutlet MKMapView*MKMapView//地图视图
@属性(强,非原子)CLLocationManager*locationManager//基于位置的服务
@属性(强,非原子)IBUIProgressView*myprogressView//控件
@属性(强,非原子)IBUILabel*distLabel//距离标签
@属性(强,非原子)NSMutableArray*trackPointArray//轨迹点阵列
@属性(强,非原子)CLLocationManager*位置坐标//位置坐标
@属性(非原子,赋值)CLLocationDegrees*currentLocation//当前位置
@属性(强,非原子)UIActionSheet*saveWalk;
@结束
这是我的m文件
m-file
#import "DetailViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Location.h" //imports the locations
#import "MathController.h"
#import "Walk.h"
static NSString * const detailSegueName = @"WalkDetails"; //walkdetails
@interface DetailViewController () <MKMapViewDelegate, CLLocationManagerDelegate, UIActionSheetDelegate,MKAnnotation>
@property (strong, nonatomic) MKMapView *mapView;
@end
@implementation DetailViewController
@synthesize MKMapView; //mapview
@synthesize trackPointArray; //trackpointarray
@synthesize coordinate;
#pragma mark - Managing the detail item
- (void)setWalk:(Walk *)walk //settings for walk
{
if (_walk != walk) {
_walk = walk;
[self configureView];
}
}
- (void)configureView
{
self.distLabel.text = [MathController stringifyDistance:self.walk.distance.floatValue];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[self loadMap];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { //request authorization
[self.locationManager requestWhenInUseAuthorization]; //request when in use
self.MKMapView.showsUserLocation =YES; //shows users location
CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
MKCoordinateRegion tRegion =MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate],500,500);
[self.MKMapView setRegion:tRegion animated:animated]; //sets region to animated
}
}
- (MKCoordinateRegion)mapRegion
{
MKCoordinateRegion region;
Location *initialLoc = self.walk.locations.firstObject;
float minLat = initialLoc.latitude.floatValue;
float minLng = initialLoc.longitude.floatValue;
float maxLat = initialLoc.latitude.floatValue;
float maxLng = initialLoc.longitude.floatValue;
for (Location *location in self.walk.locations) {
if (location.latitude.floatValue < minLat) {
minLat = location.latitude.floatValue;
}
if (location.longitude.floatValue < minLng) {
minLng = location.longitude.floatValue;
}
if (location.latitude.floatValue > maxLat) {
maxLat = location.latitude.floatValue;
}
if (location.longitude.floatValue > maxLng) {
maxLng = location.longitude.floatValue;
}
}
region.center.latitude = (minLat + maxLat) / 2.0f;
region.center.longitude = (minLng + maxLng) / 2.0f;
region.span.latitudeDelta = (maxLat - minLat) * 1.1f; // 10% padding
region.span.longitudeDelta = (maxLng - minLng) * 1.1f; // 10% padding
return region;
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolyline *polyLine = (MKPolyline *)overlay;
MKPolylineRenderer *aRenderer = [[MKPolylineRenderer alloc] initWithPolyline:polyLine];
aRenderer.strokeColor = [UIColor blackColor];
aRenderer.lineWidth = 3;
return aRenderer;
}
return nil;
//coordinates for the polyline
}
- (MKPolyline *)polyLine {
CLLocationCoordinate2D coords[self.walk.locations.count];
for (int i = 0; i < self.walk.locations.count; i++) {
Location *location = [self.walk.locations objectAtIndex:i];
coords[i] = CLLocationCoordinate2DMake(location.latitude.doubleValue, location.longitude.doubleValue);
}
return [MKPolyline polylineWithCoordinates:coords count:self.walk.locations.count];
}
//loading map
- (void)loadMap
{
if (self.walk.locations.count > 0) {
self.mapView.hidden = NO;
// set the map bounds
[self.mapView setRegion:[self mapRegion]];
// make the line(s!) on the map
[self.mapView addOverlay:[self polyLine]];
} else {
// no locations were found!
self.mapView.hidden = YES;
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Sorry, this run has no locations saved."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (IBAction)stopPressed:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""delegate:self //sets the delgate
cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil //cancel button
otherButtonTitles:@"Save", @"Discard", nil]; //button to destruct
actionSheet.actionSheetStyle = UIActionSheetStyleDefault; //actionsheet for sender
[actionSheet showInView:self.view]; //attribute to show in view
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex //action for clicked button
{
if (buttonIndex == 0) {
[self saveWalk]; //saves the walk
[self.navigationController popToRootViewControllerAnimated:YES];
//[self performSegueWithIdentifier:detailSegueName sender:nil]; /check this later if it works
} else if (buttonIndex == 1) { //discard
[self.navigationController popToRootViewControllerAnimated:YES]; //button will call rootview (homescreen)
}
}
@end
#导入“DetailViewController.h”
#进口
#进口
#导入“Location.h”//导入位置
#导入“MathController.h”
#导入“Walk.h”
静态NSString*const detailSegueName=@“WalkDetails”//详细信息
@接口详细信息ViewController()
@属性(强,非原子)MKMapView*mapView;
@结束
@实现细节视图控制器
@综合地图视图//地图视图
@综合轨迹点阵列//轨迹点阵列
@综合坐标;
#pragma标记-管理详细信息项
-(void)设置漫游:(漫游*)漫游//漫游设置
{
如果(_walk!=walk){
_走=走;
[自配置视图];
}
}
-(无效)配置视图
{
self.distLabel.text=[MathController stringifyDistance:self.walk.distance.floatValue];
NSDateFormatter*格式化程序=[[NSDateFormatter alloc]init];
[格式化程序setDateStyle:nsDateFormatTermineMiumStyle];
[自助地图];
}
-(无效)视图显示:(BOOL)动画{
[超级视图下载];
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){//请求授权
[self.locationManager在使用授权时请求];//在使用时请求
self.MKMapView.showsUserLocation=YES;//显示用户的位置
CLLocationManager*tempLocationManager=[[CLLocationManager alloc]init];
MKCoordinateRegion tRegion=MKCoordinateRegionMakeWithDistance([tempLocationManager.location坐标],500500);
[self.MKMapView setRegion:tRegion animated:animated];//将区域设置为动画
}
}
-(MKCoordinateRegion)地图区域
{
协调区域;
位置*initialLoc=self.walk.locations.firstObject;
float minLat=initialLoc.latitude.floatValue;
float minLng=initialLoc.longitude.floatValue;
float maxLat=initialLoc.latitude.floatValue;
float maxLng=initialLoc.longitude.floatValue;
用于(self.walk.locations中的位置*位置){
if(location.lation.floatValuemaxLat){
maxLat=location.latitude.floatValue;
}
如果(location.longitude.floatValue>maxLng){
maxLng=location.longitude.floatValue;
}
}
region.center.latitude=(minLat+maxLat)/2.0f;
region.center.longitude=(minLng+maxLng)/2.0f;
region.span.latitudeDelta=(maxLat-minLat)*1.1f;//10%填充
region.span.longitudeDelta=(maxLng-minLng)*1.1f;//10%填充
返回区;
}
-(MKOverlayRenderer*)地图视图:(MKMapView*)地图视图渲染器ForOverlay:(id)overlay
{
if([overlay iskindof类:[MKPolyline类]]){
MKPolyline*polyLine=(MKPolyline*)覆盖;
MKPolylineRenderer*arender=[[MKPolylineRenderer alloc]initWithPolyline:polyLine];
arender.strokeColor=[UIColor blackColor];
arender.lineWidth=3;
返回arender;
}
返回零;
//多段线的坐标
}
-(MKPolyline*)多段线{
CLLocationCoordinate2D坐标[self.walk.locations.count];
对于(int i=0;i0){
self.mapView.hidden=否;
//设置地图边界
[self.mapView setRegion:[self mapRegion]];
//在地图上画线
[self.mapView addOverlay:[self polyLine]];
}否则{
//没有找到任何位置!
self.mapView.hidden=是;
UIAlertView*alertView=[[UIAlertView alloc]
initWithTitle:@“错误”
消息:@“抱歉,此运行未保存任何位置。”
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报视图显示];
}
}
-(i动作)塞住