【问题标题】:road map from location A to location B by latitude and longitude从A点到B点的经纬度路线图
【发布时间】:2012-07-09 10:08:54
【问题描述】:

我正在处理地图和位置。我想要从一个位置到另一个位置的路线图。我提供了位置(目的地和来源)的纬度和经度。所以请帮我拿下这张地图。

我阅读了有关 Google map api 的信息,但没有找到完美的答案。

【问题讨论】:

  • 不是这样,但我尝试了很多,但我没有找到从源头到目的地的完美路线图。
  • 我想通过纬度和经度矩阵绘制地图,因为我不断跟踪用户位置我将使用这些详细信息获得纬度和经度我想从用户的起始位置绘制地图用户

标签: iphone objective-c ios maps allocation


【解决方案1】:

使用以下 google map API 的 URL 来查找两个位置之间的路线图路线

http://maps.google.com/?saddr=STARTINGADDRESS&daddr=DESTINATIONADDRESS

【讨论】:

  • 我想通过纬度和经度矩阵绘制地图,因为我不断跟踪用户位置我将使用这些详细信息获得纬度和经度我想从用户的起始位置绘制地图用户
【解决方案2】:

首先,您必须听从 Google 的指示。像这样:

NSError     *error = nil;
NSString    *directionsInJSON = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true",start.coordinate.latitude , start.coordinate.longitude , destination.coordinate.latitude, destination.coordinate.longitude]]encoding:NSUTF8StringEncoding error:&error];

之后,解析上面得到的 JSON:

if (error == nil) 
{
    NSError *JSONError = nil;
    SBJsonParser *JSONParser = [[SBJsonParser alloc] init];
    id  directions = [JSONParser objectWithString:directionsInJSON error:&JSONError];
    if (JSONError == nil) 
    {
        directions = (NSDictionary*)directions;

        NSString *status = [directions objectForKey:@"status"];
        if ([status isEqualToString:@"OK"]) 
        {
            NSArray         *routes = [directions objectForKey:@"routes"];
            NSDictionary    *route  = [routes objectAtIndex:0];
            NSDictionary    *polylineOverview = [route objectForKey:@"overview_polyline"];

            NSString *polylinePoints = [polylineOverview objectForKey:@"points"];
            NSArray *decodedPolyline = [self decodePolyLine:polylinePoints];

            [self loadRouteWithPoints:decodedPolyline];
        }
        else
        {
            UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"No routes found" message:@"Sorry , we couldn't find any routes between you two." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
            [alert show];
        }
    }
}
else
{
    NSLog(@"error: %@",error.description);
}

现在你需要这些方法:

-(void) loadRouteWithPoints:(NSArray *) routes
{

    CLLocationCoordinate2D coords[[routes count]];
    for(int i = 0; i < routes.count; i++)
    {
        CLLocation* loc = (CLLocation*)[routes objectAtIndex:i];//[NSKeyedUnarchiver unarchiveObjectWithData:[routes objectAtIndex:i]];
        CLLocationCoordinate2D c;
        c.latitude  = loc.coordinate.latitude;
        c.longitude = loc.coordinate.longitude;
        coords[i]   = c;
    }

    MKPolyline *line = [[MKPolyline polylineWithCoordinates:(CLLocationCoordinate2D*)coords count:[routes count]] retain];
    [self.mapsView addOverlay:line];
}

还有:

- (NSMutableArray *)decodePolyLine: (NSString *)encoded 
{
    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    *latitude   = [[[NSNumber   alloc] initWithFloat:lat * 1e-5] autorelease];
        NSNumber    *longitude  = [[[NSNumber   alloc] initWithFloat:lng * 1e-5] autorelease];
        CLLocation  *loc        = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
        [array addObject:loc];
    }

    return array;
}

瞧!

干杯!

【讨论】:

  • 我想通过纬度和经度矩阵绘制地图,因为我不断跟踪用户位置我将使用这些详细信息获得纬度和经度我想从用户的起始位置绘制地图用户
  • 当您跟踪用户位置时,您可以更新已添加的注释(路线)或将其删除并从该新位置创建一个新注释。我不明白,你想用位置矩阵做什么?
  • 谷歌地图将显示从源纬度和经度到目的地纬度和经度的适当路径,但我想制作跟随用户的路径,用户路径可能不是适当的路径。
  • 当用户位置改变时(你需要 CLLocationManagerDelegate 方法)你应该更新路由。让谷歌给你从用户位置到目的地的路线。由于用户位置在变化,路线也会发生变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多