0
我新的iOS開發,我使用的XCode 6.如何使用mapKit點之間畫線在Xcode 6
我想在地圖上的兩個位置之間畫一條路線,就像一個導遊。當遊客點擊另一個地點時,我希望能夠繪製路線;並告知距離當前位置的距離。
我新的iOS開發,我使用的XCode 6.如何使用mapKit點之間畫線在Xcode 6
我想在地圖上的兩個位置之間畫一條路線,就像一個導遊。當遊客點擊另一個地點時,我希望能夠繪製路線;並告知距離當前位置的距離。
據我所知你的問題是你想繪製兩個地點之間的路線。 要實現此操作,請先製作兩個UITexfield
以獲取源和目標,然後使用一個UIButton
執行操作。 連接所有網點
不要Forger添加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
謝謝你UnicoRahul。 – 2015-02-19 10:42:40
我有一個問題:它處理你喜歡的實現? – MinuMaster 2015-02-19 10:42:55
1)MKMapKit方向API。 2)用於繪製路徑的Google路由API。 – MinuMaster 2015-02-19 10:43:12
繪製路徑的Google路由API。 – 2015-02-19 10:46:40