【发布时间】:2014-11-10 20:39:34
【问题描述】:
我收到了 Json 响应。这里是:
Directions {
routes = (
{
bounds = {
northeast = {
lat = "39.893397";
lng = "42.2477961";
};
southwest = {
lat = "21.4204199";
lng = "39.2398944";
};
};
copyrights = "Map data \U00a92014 Google, Mapa GISrael, ORION-ME";
legs = (
{
distance = {
text = "2,940 km";
value = 2940341;
};
duration = {
text = "1 day 15 hours";
value = 138793;
};
"end_address" = "Unnamed Road, Erzincan Province, Turkey";
"end_location" = {
lat = "39.893397";
lng = "39.906191";
};
"start_address" = "4289, Al Hajlah, Mecca 24231\U00a06970, Saudi Arabia";
"start_location" = {
lat = "21.4204199";
lng = "39.8258119";
};
steps = (
{
distance = {
text = "0.6 km";
value = 630;
};
duration = {
text = "1 min";
value = 55;
};
"end_location" = {
lat = "21.4235672";
lng = "39.8211613";
};
"html_instructions" = "Head <b>west</b> on <b>Alsouq Alsagheer Tunnel</b>";
polyline = {
points = "stvaCinarFEh@?h@?f@Cd@If@Gb@Yx@Yx@e@bAWb@Y`@CD[b@c@l@ILADIRIVGPKRGJKPILKPWRi@X_Ad@MFSJ]N[N_@P]ROJ";
};
"start_location" = {
lat = "21.4204199";
lng = "39.8258119";
};
"travel_mode" = DRIVING;
然后继续……
这是我项目中的一些代码:
方向.h
-(void)retrieveDirectionsFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination
{
NSString *directionsURL=[NSString stringWithFormat:@"http://maps.google.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",origin.latitude,origin.longitude,destination.longitude,destination.longitude];
_directionsURL=[NSURL URLWithString:directionsURL];
[self retrieveDirections:nil withDelegate:self];
}
-(void)retrieveDirections:(SEL)selector withDelegate:(id)delegate{
dispatch_async(dispatch_get_main_queue(), ^{
NSData *data = [NSData dataWithContentsOfURL:_directionsURL];
[self fetchedData:data withDelegate:delegate];
});
}
-(void)fetchedData:(NSData *)data withDelegate:(id)delegate{
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Directions %@",json);
if(!error){
self.steps = json[@"routes"][0][@"legs"][0][@"steps"];
}
//MapViewController.m
marker.position = CLLocationCoordinate2DMake(21.422492, 39.826169);
marker2.position = CLLocationCoordinate2DMake(21.413333, 39.893333);
marker.map = mapView;
marker2.map = mapView;
directions=[[Directions alloc]init];
[directions retrieveDirectionsFromOrigin:marker.position toDestination:marker2.position];
[mapViewView setNeedsDisplay];
[mapView setNeedsDisplay];
[self.view bringSubviewToFront:backButton];
}
我是初学者。请让我知道接下来要做什么以通过折线显示路线。谢谢
【问题讨论】:
-
不要从 NSJSONSerialization(或任何其他具有
error参数的 API)测试error。测试结果,如果为 nil,则转储error以显示失败原因。 -
否则,接下来要做的就是想一个问题。
标签: ios json google-maps parsing polyline