该实现代码包括三个类:MapController、Place、PlaceMark.直接调用mapcontroller类传入目的地坐标;
| |
|
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import<CoreLocation/CLLocation.h>
#import <MapKit/MapKit.h>
#import "ASIHTTPRequest.h"
#import "Place.h"
#import "PlaceMark.h"
//#import "RegexKitLite.h"
@interface MapViewController : UIViewController<CLLocationManagerDelegat
}
@property (nonatomic, retain) MKMapView *map;
@property (nonatomic, retain) CLLocationManager *locManager;
@property(nonatomic,retain)ASIHTTPRequest *request;
@property(nonatomic,retain)NSArray *routes;
@property(nonatomic,retain)UIImageView *route_view;
@property(nonatomic,retain)UIColor *line_color;
@property(nonatomic,retain)MKPolyline* routelay;
@property(assign)CGFloat latitude;
@property(assign)CGFloat longitude;
-(void) showRouteFrom: (Place*) f to:(Place*) t ;
@end
MapViewController.h类:
#import "MapViewController.h"
#import "SBJson.h"
@interface MapViewController(PrivateMethod)
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded;
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t ;
-(void) updateRouteView ;
-(void) centerMap;
@end
@implementation MapViewController
@synthesize map;
@synthesize locManager;
@synthesize request=_request;
@synthesize routes=_routes;
@synthesize route_view=_route_view;
@synthesize line_color=_line_color;
@synthesize routelay =
@synthesize longitude;
@synthesize latitude;
- (void)viewDidLoad {
[backButton setBackgroundImage:[UIImage imageNamed:@"naviback.png"]
[backButton setTitle:@" 返回" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont systemFontOfSize:13];
[backButton addTarget:self action:@selector(navigationButtonPressed)
forControlEvents:UIControlEventTouchUpIns
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[backButton release];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
}
-(void)navigationButtonPressed{
}
-(void)viewWillAppear:(BOOL)animated
{
//设置精度,越小越精确
}
- (void)dealloc {
}
#pragma mark -
#pragma mark Core Location Delegate Methods
- (void)locationManager:(CLLocationManager *)manager
//
//
//
}
-(void) showRouteFrom: (Place*) f to:(Place*) t {
if(_routes) {
[map removeAnnotations:[map annotations]];
[_routes release];
}
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:f] autorelease];
PlaceMark* to = [[[PlaceMark alloc] initWithPlace:t] autorelease];
[map addAnnotation:from];
[map addAnnotation:to];
_routes = [[self calculateRoutesFrom:from.coordinate to:to.coordinate] retain];
[self updateRouteView];
// [self centerMap];
}
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
}
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfStri
options:NSLiteralSearch
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude1 = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitude1 = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
printf("[%f,", [latitude1 doubleValue]);
printf("%f]", [longitude1 doubleValue]);
CLLocation *loc1 = [[[CLLocation alloc] initWithLatitude:[latitude1 floatValue] longitude:[longitude1 floatValue]] autorelease];
[array addObject:loc1];
}
return array;
}
-(void) centerMap {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < _routes.count; idx++)
CLLocation* currentLocation = [_routes objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
region.center.latitude
region.center.longitude
region.span.latitudeDelta
region.span.longitudeDelta = maxLon - minLon;
[map setRegion:region animated:YES];
}
-(void) updateRouteView {
CGContextRef context = CGBitmapContextCreate(nil,
CGContextSetStrokeColorW
CGContextSetRGBFillColor
CGContextSetLineWidth(context, 5.0);
for(int i = 0; i < _routes.count; i++) {
CLLocation* location = [_routes objectAtIndex:i];
CGPoint point = [map convertCoordinate:location.coordinate toPointToView:_route_view];
if(i == 0) {
CGContextMoveToPoint(context, point.x, _route_view.frame.size.height - point.y);
} else {
CGContextAddLineToPoint(context, point.x, _route_view.frame.size.height - point.y);
}
}
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateIma
UIImage* img = [UIImage imageWithCGImage:image];
_route_view.image = img;
CGContextRelease(context);
}
#pragma mark mapView delegate functions
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated
{
_route_view.hidden = YES;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
[self updateRouteView];
_route_view.hidden = NO;
[_route_view setNeedsDisplay];
}
@end
//
//
//
//
//
//
//
#import <Foundation/Foundation.h>
@interface Place : NSObject {
NSString* name;
NSString* description;
double latitude;
double longitude;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* description;
@property (nonatomic) double latitude;
@property (nonatomic) double longitude;
@end
//
//
//
//
//
//
//
#import "Place.h"
@implementation Place
@synthesize name;
@synthesize description;
@synthesize latitude;
@synthesize longitude;
- (void) dealloc
{
[name release];
[description release];
[super dealloc];
}
@end
//
//
//
//
//
//
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "Place.h"
@interface PlaceMark : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
Place* place;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) Place* place;
-(id) initWithPlace: (Place*) p;
@end
//
//
//
//
//
//
//
import
@implementation
@synthesize
@synthesize
-(id) initWithPlace: (Place*) p
{
self
if
coordinate.latitude
coordinate.longitude
self.place
}
return
}
- (NSString
{
return
}
- (NSString
{
return
}
- (void) dealloc
{
[place
[super
}
@end#