由数组提取的Google地图上的地点位置

时间:2013-10-24 06:25:30

标签: ios iphone google-maps

朋友你好    的谷歌地图-SDK

我是ios的新手,正在使用GOOGLE-MAP-SDK。一切都进展顺利,但我无法在这种情况下使用注释,因为我无法找到我从google的placeAPI获取的位置。   所以请帮助我解决我的错误。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~

代码文件

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~

-(IBAction)search:(id)sender
{

    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=30.7343000,76.7933000&radius=500&types=food&name&sensor=true&key=AIzaSyCGeIN7gCxU8baq3e5eL0DU3_JHeWyKzic"];

     //Formulate the string as URL object.
     NSURL *googleRequestURL=[NSURL URLWithString:url];

     // Retrieve the results of the URL.
     dispatch_async(kBgQueue, ^{
          NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
          [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData

                      options:kNilOptions
                      error:&error];

    //The results from Google will be an array obtained from the NSDictionary object with the key "results".
    NSArray* places = [json objectForKey:@"results"];

    //Write out the data to the console.
    NSLog(@"Google Data: %@", places);

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

以下是我将从中回收数据并希望在googlemap上显示数据的代码。  NSLog(@“Google Data:%@”,地点);给了我出局......

1 个答案:

答案 0 :(得分:1)

...
        NSArray *markerForplaces = [NSMutableArray arrayWithCapacity:[responseResults count]];
        for (NSDictionary *dict in responseResults) {
            GMSMarker *markerForplace = [self markerForGooglePlaceFromDictionary:dict error:nil];
            if (markerForplace) {
                [markerForplaces addObject:markerForplace];
                                    markerForplace.map = self.mapView;
            }
        }
...

- (GMSMarker*)markerForGooglePlaceFromDictionary:(NSDictionary*)dict {
         CLLocationCoordinate2D coordinate = [self coordinateForPlace:dict];
         GMSMarker *marker = [GMSMarker markerWithPosition:coordinate];
         marker.userInfo = dict; //save place
}


- (CLLocationCoordinate2D)coordinateForPlace:(NSDictionary*)dictionary {
    NSDictionary *geo = [dictionary objectForKey:@"geometry"];
    if ([geo isKindOfClass:[NSDictionary class]]) {
        NSDictionary *loc = [geo objectForKey:@"location"];
        if ([loc isKindOfClass:[NSDictionary class]]) {
            NSString *lat = [loc objectForKey:@"lat"];
            NSString *lng = [loc objectForKey:@"lng"];
            return CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]);
        }
    }
    return CLLocationCoordinate2DMake(0, 0);
}