我正在使用地圖和位置進行工作。我想要從一個地方到另一個地方有路線圖。我提供的位置(目的地和來源)的緯度和經度。所以請幫我拿這張地圖。經緯度從位置A到位置B的路線圖
我看過谷歌地圖API,但沒有找到完美的答案。下面的谷歌地圖API的URL
我正在使用地圖和位置進行工作。我想要從一個地方到另一個地方有路線圖。我提供的位置(目的地和來源)的緯度和經度。所以請幫我拿這張地圖。經緯度從位置A到位置B的路線圖
我看過谷歌地圖API,但沒有找到完美的答案。下面的谷歌地圖API的URL
首先,你必須採取從谷歌的方向之間的路線圖路線。就像這樣:
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;
}
瞧!
乾杯!
在這裏所以好的答案來解決好問題。這個問題在許多方面都不成問題:未能顯示自己的代碼(無論如何破壞),需求的模糊性以及您希望其他人爲您編寫代碼的提示。 – 2012-07-09 10:16:47
這不是那樣的,但我嘗試了很多,但我沒有找到從源到目的地的完美路線圖。 – nids 2012-07-09 10:47:56
我想通過緯度和經度矩陣作爲即時跟蹤用戶位置的地圖,我將繼續使用這些細節來獲取緯度和經度我想繪製從用戶起始位置到用戶當前位置的地圖 – nids 2012-07-11 09:26:10