私は地図と場所で仕事をしています。ある場所から別の場所へのロードマップが欲しいのですが。場所(宛先とソース)の両方の緯度と経度を提供しています。だから私がこの地図を持っているのを手伝ってください。
Google map apiについて読みましたが、完璧な答えは見つかりませんでした。
私は地図と場所で仕事をしています。ある場所から別の場所へのロードマップが欲しいのですが。場所(宛先とソース)の両方の緯度と経度を提供しています。だから私がこの地図を持っているのを手伝ってください。
Google map apiについて読みましたが、完璧な答えは見つかりませんでした。
以下の Google マップ API の URL を使用して、2 つの場所間のロードマップ ルートを検索します。
http://maps.google.com/?saddr=STARTINGADDRESS&daddr=DESTINATIONADDRESS
まず、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;
}
出来上がり!
乾杯!