嗨,我是Ios的初学者,在我的项目中,我试图在用户步行或沿着道路行驶时绘制路线,但路线没有正确绘图,请帮我一些(如果有任何机构有想法请修改我的代码和发布工作代码)
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
{
IBOutlet MKMapView *myMap;
CLLocationManager *locmanager;
}
@property (strong, nonatomic) IBOutlet MKMapView *myMap;
@property (nonatomic, strong) NSMutableArray *allPinsArr;
@property (nonatomic, strong) MKPolylineView *lineView;
@property (nonatomic, strong) MKPolyline *polyline;
- (IBAction)startButton:(id)sender;
@end
#import "ViewController.h"
#import "Location.h"
#import "Pin.h"
@interface ViewController ()
{
CLLocationCoordinate2D curentLocation;
CLLocation *oldLocation1;
CLLocation *newLocation1;
NSUserDefaults *def;
BOOL isfirstRun;
}
@end
@implementation ViewController
@synthesize myMap,allPinsArr,polyline,lineView;
- (void)viewDidLoad {
[super viewDidLoad];
myMap.delegate = self;
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
if ([locmanager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[locmanager requestAlwaysAuthorization];
}
[locmanager setDistanceFilter:10.0f];
[locmanager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[myMap setShowsUserLocation:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)startButton:(id)sender {
[myMap removeOverlay:self.polyline];
[lineView removeFromSuperview];
[myMap removeAnnotations:myMap.annotations];
allPinsArr = [[NSMutableArray alloc]init];
[locmanager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
newLocation1 = newLocation;
oldLocation1 = oldLocation;
curentLocation = newLocation.coordinate;
[self checkthelocationisPreviousornot:newLocation :oldLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
// check new location is previous or not
-(void)checkthelocationisPreviousornot:(CLLocation *)newLocation :(CLLocation *)oldLocation {
//For Sqlite Reference
NSInteger idVal = [def integerForKey:@"id"];
if (idVal==0) {
idVal=1;
}
[def setInteger:idVal forKey:@"id"];
if ((newLocation.coordinate.latitude!=oldLocation.coordinate.latitude || newLocation.coordinate.longitude!=oldLocation.coordinate.longitude) || isfirstRun==YES) {
double distance =[oldLocation distanceFromLocation:newLocation];
NSString *distStr = [NSString stringWithFormat:@"%f",distance];
//Adding data in sqlite
// [self insertcoordinatestodatabase:curentLocation :idVal :@"" :@""];
[self addingCoordinatesToarray:newLocation.coordinate :distStr];
}
}
//add co-ordinates to array
-(void)addingCoordinatesToarray:(CLLocationCoordinate2D )coordinates :(NSString *)distance {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinates, 1000, 1000);
[myMap setRegion:region animated:YES];
Pin *pin =[[Pin alloc] initWithCoordinate:coordinates title:@"Entry point" distance:distance myimage:nil videopath:nil];
[myMap addAnnotation:pin];
[allPinsArr addObject:pin];
[self drawline];
}
-(void)drawline{
[self drawLineSubroutine];
[self drawLineSubroutine];
}
- (void)drawLineSubroutine {
// remove polyline if one exists
[myMap removeOverlay:self.polyline];
// create an array of coordinates from allPins
CLLocationCoordinate2D coordinates[allPinsArr.count];
int i = 0;
for (Pin *currentLoc in allPinsArr) {
coordinates[i] = currentLoc.coordinate;
i++;
}
// create a polyline with all cooridnates
polyline = [MKPolyline polylineWithCoordinates:coordinates count:allPinsArr.count];
[myMap addOverlay:polyline];
self.polyline = polyline;
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.polyline)
{
//if we have not yet created an overlay view for this overlay, create it now.
if (self.lineView) {
[self.lineView removeFromSuperview];
}
self.lineView = [[MKPolylineView alloc] initWithPolyline:self.polyline];
self.lineView.fillColor = [UIColor redColor];
self.lineView.strokeColor = [UIColor greenColor];
self.lineView.lineWidth = 10;
overlayView = self.lineView;
}
return overlayView;
}
@end
#import <Foundation/Foundation.h>
@import MapKit;
@interface Location : NSObject<MKAnnotation>{
CLLocationCoordinate2D coordinate;
NSString *loctionName;
NSString *distance;
NSString *mediaPath;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *loctionName;
@property (nonatomic, retain) NSString *distance;
@property (nonatomic, retain) NSString *mediaPath;
@end
#import "Location.h"
@implementation Location
@synthesize coordinate,loctionName,distance,mediaPath;
@end
#import <Foundation/Foundation.h>
@import MapKit;
@interface Pin : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) int wayPointID;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *distance;
@property (nonatomic, copy) UIImage *myimage;
@property (nonatomic, copy) NSString *videopath;
- (id)initWithCoordinate:(CLLocationCoordinate2D)newCoordinate title:(NSString *)title distance:(NSString *)distance myimage:(UIImage *)myimage videopath:(NSString *)videopath;
@end
#import "Pin.h"
@implementation Pin
- (id)initWithCoordinate:(CLLocationCoordinate2D)newCoordinate title:(NSString *)title distance:(NSString *)distance myimage:(UIImage *)myimage videopath:(NSString *)videopath{
self = [super init];
if (self) {
_coordinate = newCoordinate;
_title = title;
_distance = distance;
_myimage = myimage;
_videopath = videopath;
}
return self;
}
@end