我是iOS开发新手,我正在使用XCode 6.
我想在地图上的两个位置之间绘制一条路线,就像导游一样。当游客点击另一个位置时,我希望能够画一条路线;以及通知当前位置的距离。
答案 0 :(得分:0)
据我所知,你的问题是你想在两个地点之间画一条路线。
要实现这一点,首先要使两个UITexfield
获取源和目标,并使用一个UIButton
来执行操作。
连接所有插座
不要伪造添加Map Framework。
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate>
{
IBOutlet UITextField *source1;
IBOutlet UITextField *destination1;
IBOutlet MKMapView *map;
MKAnnotationView *Annotation;
}
-(CLLocationCoordinate2D)FindCoordinates:(NSString *)place;
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay;
-(IBAction)getLocation:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
CLLocationCoordinate2D myLocation;
CLLocationCoordinate2D sourceLocation;
CLLocationCoordinate2D destinationLocation;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(CLLocationCoordinate2D)FindCoordinates:(NSString *)place
{
NSString *addresss = place;
NSString *esc_addr = [addresss stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat: @"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];
NSDictionary *googleResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *resultsDict = [googleResponse valueForKey: @"results"]; // get the results dictionary
NSDictionary *geometryDict = [ resultsDict valueForKey: @"geometry"]; // geometry dictionary within the results dictionary
NSDictionary *locationDict = [ geometryDict valueForKey: @"location"]; // location dictionary within the geometry dictionary
// nslo (@”– returning latitude & longitude from google –”);
NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject]; // (one element) array entries provided by the json parser
NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject]; // (one element) array entries provided by the json parser
myLocation.latitude = [latString doubleValue]; // the json parser uses NSArrays which don’t support “doubleValue”
myLocation.longitude = [lngString doubleValue];
return myLocation;
}
-(IBAction)getLocation:(id)sender
{
sourceLocation = [self FindCoordinates:source1.text];
NSLog(@"%@",[NSString stringWithFormat:@"Source lat:%f Source lon:%f",sourceLocation.latitude,sourceLocation.longitude]);
destinationLocation = [self FindCoordinates:destination1.text];
NSLog(@"%@",[NSString stringWithFormat:@"Desti lat:%f Desti lon:%f",destinationLocation.latitude,destinationLocation.longitude]);
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:sourceLocation.latitude longitude:sourceLocation.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:destinationLocation.latitude longitude:destinationLocation.longitude];
CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
NSLog(@"%@",[NSString stringWithFormat:@"Distance iin KMeteres %f",distance/1000]); // Gives me air distance
}
@end