问题描述
我需要在Google地图视图中找到一个地方到另一个地方的路径。如何使用谷歌地图sdk iOS绘制方向图。
可以给出任何源代码。然后解释如何实现这一点。
下面我也附加了图片,我需要使用Google Map SDK iOS在iPhone应用中实现此功能。
谢谢
NSString * pointStr = [[[[routeDict objectForKey:@routes] objectAtIndex:0] objectForKey:@overview_polyline] objectForKey:@points];float max_long = 0.0;
float min_long = 0.0;
float max_lat = 0.0;
float min_lat = 0.0;
NSMutableArray * routeArr = [self decodePolyLine:pointStr];
CLLocationCoordinate2D commuterLotCoords [[routeArr count]];
CLLocation * loc;
if([routeArr count]){
loc = [routeArr objectAtIndex:0];
max_long = loc.coordinate.longitude;
min_long = loc.coordinate.longitude;
max_lat = loc.coordinate.latitude;
min_lat = loc.coordinate.latitude;
}
for(int i = 0; i< [routeArr count]; i ++){
CLLocation * loc = [routeArr objectAtIndex:i];
commuterLotCoords [i] = loc.coordinate;
if(loc.coordinate.latitude> max_lat){
max_lat = loc.coordinate.latitude;
}
if(loc.coordinate.latitude< min_lat){
min_lat = loc.coordinate.latitude;
}
if(loc.coordinate.longitude> max_long){
max_long = loc.coordinate.longitude;
}
if(loc.coordinate.longitude< min_long){
min_long = loc.coordinate.longitude;
$ b}
MKPolyline * overflowRoutePolygon = [MKPolyline polylineWithCoordinates:commuterLotCoords count:[routeArr count]] ;
[mapView addOverlay:overflowRoutePolygon];
// NSLog(@%f%f%f%f,min_lat,max_lat,min_long,max_long);
if(max_lat == 0.0 || min_lat == 0.0 || max_long == 0.0 || min_long == 0.0){
} else {
//计算地图中心
float center_long =(max_long + min_long)/ 2;
float center_lat =(max_lat + min_lat)/ 2;
//计算增量
float deltaLat = max_lat - min_lat + .00032;
float deltaLong = max_long - min_long + .00032;
// NSLog(@%f%f%f%f,center_lat,center_long,deltaLat,deltaLong);
//创建新区域并设置地图
CLLocationCoordinate2D坐标;
cordinate.latitude = center_lat;
cordinate.longitude = center_long;
MKCoordinateSpan span = MKCoordinateSpanMake(deltaLat,deltaLong);
MKCoordinateRegion region = {cordinate,span};
[mapView setRegion:region];
- (void)showRoute
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
float sourceLat = 0.00;
float sourceLong = 0.00;
float destinationLat = 0.00;
float destinationLong = 0.00;
id record = [routeArray objectAtIndex:selectedIndex];
sourceLat = [[[[record objectForKey:@To] objectForKey:@Latitude] objectForKey:@text] floatValue];
sourceLong = [[[[record objectForKey:@To] objectForKey:@Longitude] objectForKey:@text] floatValue];
destinationLat = [[[[record objectForKey:@From] objectForKey:@Latitude] objectForKey:@text] floatValue];
destinationLong = [[[[record objectForKey:@From] objectForKey:@Longitude] objectForKey:@text] floatValue];
if(sourceLat == 0.00 || sourceLong == 0.00 || destinationLat == 0.00 || destinationLong == 0.00){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@ 方向错误
消息:@您的目的地之一无效。
委托:self
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];
[警戒释放];
return;
}
NSString * urlStr = [NSString stringWithFormat:@http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination =%f,%f& sensor = false,
sourceLat,sourceLong,
destinationLat,destinationLong];
NSLog(@urlStr:%@,urlStr);
NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
// NSLog(@direction response:%@,[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
JSONDecoder * jsonKitDecoder = [JSONDecoder decoder];
NSMutableDictionary * routeDic = [[jsonKitDecoder objectWithData:data] copy];
if(![[[routeDic objectForKey:@status] uppercaseString] isEqualToString:@OK]){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Direction Error
消息:@无法从您的默认位置找到方向。
委托:self
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];
[警戒释放];
[游泳池发行];
return;
}
[self performSelectorOnMainThread:@selector(showRoute :) withObject:routeDic waitUntilDone:YES]; [b
$ b [池释放];
return;
$ b - (NSMutableArray *)decodePolyLine:(NSString *)encodedStr
{
NSMutableString * encoded = [[NSMutableString alloc ] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[编码的replaceOccurrencesOfString:@\\\\withString:@\\options:NSLiteralSearch range:NSMakeRange(0,[encoded length])];
NSInteger len = [编码长度];
NSInteger index = 0;
NSMutableArray * array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat = 0;
NSInteger lng = 0;
while(index< len){
NSInteger b;
NSInteger shift = 0;
NSInteger结果= 0;
do {
b = [encoded characterAtIndex:index ++] - 63;
result | =(b& 0x1f)<<转移;
shift + = 5;
} while(b> = 0x20);
NSInteger dlat =((result& 1)?〜(result>> 1):(result>> 1));
lat + = dlat;
shift = 0;
结果= 0;
do {
b = [encoded characterAtIndex:index ++] - 63;
result | =(b& 0x1f)<<转移;
shift + = 5;
} while(b> = 0x20);
NSInteger dlng =((result& 1)?〜(result>> 1):(result>> 1));
lng + = dlng;
NSNumber * latitute = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber * longitute = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
CLLocation * loc = [[[CLLocation alloc] initWithLatitude:[latitute floatValue] longitude:[longitute floatValue]] autorelease];
[array addObject:loc];
}
[编码发布];
返回数组;
$ p
$ b
编译标记MKMapKitDelegate
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id< MKAnnotation>)注解
{
HAnnotation * ann =(HAnnotation *)注释;
static NSString * AnnotationViewID = @annotationViewID;
HAnnotationView * annotationView =(HAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
{
annotationView = [[[HAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID type:ann.type] autorelease];
}
annotationView.type = ann.type;
annotationView.annotation =注解;
[annotationView setNeedsDisplay];
返回annotationView;
$ b - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView * aV;
(视图中的aV){
MKAnnotationView * annotationView = aV;
annotationView.canShowCallout = NO; $ MK
$ b - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id< MKOverlay>)overlay
$ b $ if([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineView * view = [[MKPolylineView alloc] initWithOverlay:overlay] ;
view.lineWidth = 2;
view.strokeColor = [UIColor blueColor];
view.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
return [view autorelease];
} else {
MKPolygonView * view = [[MKPolygonView alloc] initWithOverlay:overlay];
view.lineWidth = 2;
view.strokeColor = [UIColor yellowColor];
view.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.3];
return [view autorelease];
}
return nil;
}
试试这段代码我已经在我的项目中使用了这段代码,它工作正常。
首先在google api中传递源码和目的地的经纬度,您将获得该地点和使用多线方法解析之后的所有点,您将绘制line.simply使用此方法根据变量进行更改并数据。
i need to find path in google map view one place to another place. how to draw a direction map using google map sdk iOS.
can any of give source code. and then explain how to achieve this.
Below i attached image also, i need to achieve this in iPhone app using Google Map SDK iOS.
Thanks,
-(void)showRoute:(id)routeDict {
NSString *pointStr = [[[[routeDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];
float max_long = 0.0;
float min_long = 0.0;
float max_lat = 0.0;
float min_lat = 0.0;
NSMutableArray *routeArr = [self decodePolyLine:pointStr];
CLLocationCoordinate2D commuterLotCoords[[routeArr count]];
CLLocation *loc;
if ([routeArr count]) {
loc = [routeArr objectAtIndex:0];
max_long = loc.coordinate.longitude;
min_long = loc.coordinate.longitude;
max_lat = loc.coordinate.latitude;
min_lat = loc.coordinate.latitude;
}
for (int i=0; i<[routeArr count]; i++) {
CLLocation *loc = [routeArr objectAtIndex:i];
commuterLotCoords[i] = loc.coordinate;
if (loc.coordinate.latitude > max_lat) {
max_lat = loc.coordinate.latitude;
}
if (loc.coordinate.latitude < min_lat) {
min_lat = loc.coordinate.latitude;
}
if (loc.coordinate.longitude > max_long) {
max_long = loc.coordinate.longitude;
}
if (loc.coordinate.longitude < min_long) {
min_long = loc.coordinate.longitude;
}
}
MKPolyline *overflowRoutePolygon = [MKPolyline polylineWithCoordinates:commuterLotCoords count:[routeArr count]];
[mapView addOverlay:overflowRoutePolygon];
//NSLog(@"%f %f %f %f",min_lat,max_lat,min_long,max_long);
if ( max_lat == 0.0 || min_lat == 0.0 || max_long == 0.0 || min_long == 0.0 ) {
} else {
//calculate center of map
float center_long = (max_long + min_long) / 2;
float center_lat = (max_lat + min_lat) / 2;
//calculate deltas
float deltaLat = max_lat - min_lat + .00032;
float deltaLong = max_long - min_long + .00032;
//NSLog(@"%f %f %f %f",center_lat,center_long,deltaLat,deltaLong);
//create new region and set map
CLLocationCoordinate2D cordinate;
cordinate.latitude = center_lat;
cordinate.longitude = center_long;
MKCoordinateSpan span = MKCoordinateSpanMake(deltaLat, deltaLong);
MKCoordinateRegion region = {cordinate, span};
[mapView setRegion:region];
}
}
-(void)showRoute
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float sourceLat = 0.00;
float sourceLong = 0.00;
float destinationLat = 0.00;
float destinationLong = 0.00;
id record = [routeArray objectAtIndex:selectedIndex];
sourceLat = [[[[record objectForKey:@"To"] objectForKey:@"Latitude"] objectForKey:@"text"] floatValue];
sourceLong = [[[[record objectForKey:@"To"] objectForKey:@"Longitude"] objectForKey:@"text"] floatValue];
destinationLat = [[[[record objectForKey:@"From"] objectForKey:@"Latitude"] objectForKey:@"text"] floatValue];
destinationLong = [[[[record objectForKey:@"From"] objectForKey:@"Longitude"] objectForKey:@"text"] floatValue];
if ( sourceLat == 0.00 || sourceLong == 0.00 || destinationLat == 0.00 || destinationLong == 0.00 ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Direction Error"
message: @"One of your destination is not valid."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
return;
}
NSString *urlStr = [NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",
sourceLat, sourceLong,
destinationLat,destinationLong];
NSLog(@"urlStr : %@",urlStr);
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
//NSLog(@"direction response : %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];
NSMutableDictionary *routeDic = [[jsonKitDecoder objectWithData:data] copy];
if ( ![[[routeDic objectForKey:@"status"] uppercaseString] isEqualToString:@"OK"] ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Direction Error"
message: @"Not able to find direction from your default location."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
[pool release];
return;
}
[self performSelectorOnMainThread:@selector(showRoute:) withObject:routeDic waitUntilDone:YES];
[pool release];
return;
}
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr
{
NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
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 *latitute = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitute = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitute floatValue] longitude:[longitute floatValue]] autorelease];
[array addObject:loc];
}
[encoded release];
return array;
}
pragma mark MKMapKitDelegate
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
HAnnotation *ann = (HAnnotation *)annotation;
static NSString *AnnotationViewID = @"annotationViewID";
HAnnotationView *annotationView = (HAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[HAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID type:ann.type] autorelease];
}
annotationView.type = ann.type;
annotationView.annotation = annotation;
[annotationView setNeedsDisplay];
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *aV;
for (aV in views) {
MKAnnotationView* annotationView = aV;
annotationView.canShowCallout = NO;
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay
{
if( [overlay isKindOfClass:[MKPolyline class]] ) {
MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
view.lineWidth=2;
view.strokeColor=[UIColor blueColor];
view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
return [view autorelease];
} else {
MKPolygonView *view = [[MKPolygonView alloc] initWithOverlay:overlay];
view.lineWidth=2;
view.strokeColor=[UIColor yellowColor];
view.fillColor=[[UIColor yellowColor] colorWithAlphaComponent:0.3];
return [view autorelease];
}
return nil;
}
Try this code i have used this code in my project it is working fine.First of all pass source and destination latitude and longitude in the google api you will get all the points between that place and after parsing using poly line method you will draw line.simply use this methods make changes according to your variable and data .
这篇关于如何在谷歌地图sdk iOS中绘制一个地方到另一个地方的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!