在我的 iphone 应用程序中,我正在使用 google places api - 在 mapView 上绘制附近的餐馆。我遇到的问题是当我在同一个 mapView 上处理核心数据对象时。澄清一下:用户可以选择一个图钉(餐厅)来加载该餐厅的详细信息屏幕(他们可以在其中保存评论、评级等)——然后我将该餐厅保存为核心数据对象。但是,下次加载 mapView 时,我不想显示来自 google api 搜索的普通 mapPoint 餐厅,而是显示保存的餐厅核心数据 mapPoint(针对该位置)。
以下是一些相关代码:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//[self updateRestaurants];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Restaurant" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSError *error;
foundObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
//[self.mapView addAnnotations:foundObjects];
[self performSelector:@selector(queryGooglePlaces) withObject:nil afterDelay:2.0];
}
- (void)updateRestaurants
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Restaurant" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSError *error;
foundObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
//[self.mapView addAnnotations:foundObjects];
}
- (void)queryGooglePlaces
{
// Build the url string we are going to sent to Google. NOTE: The kGOOGLE_API_KEY is a constant which should contain your own API key that you can obtain from Google. See this link for more info:
// https://developers.google.com/maps/documentation/places/#Authentication
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%d&type=restaurant&sensor=true&key=%@", currentCenter.latitude, currentCenter.longitude, 1000 /*[NSString stringWithFormat:@"%i", currentDist]*/, kGOOGLE_API_KEY];
//Formulate the string as URL object.
//NSURL *googleRequestURL=[NSURL URLWithString:url];
NSURL *googleRequestURL = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
// 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
{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *places = [json objectForKey:@"results"];
[self plotPositions:places];
}
- (void)plotPositions:(NSArray *)data {
for (id<MKAnnotation> annotation in mapView.annotations)
{
if ([annotation isKindOfClass:[MapPoint class]])
{
[mapView removeAnnotation:annotation];
}
}
//Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++)
{
//Retrieve the NSDictionary object in each index of the array.
NSDictionary* place = [data objectAtIndex:i];
//There is a specific NSDictionary object that gives us location info.
NSDictionary *geo = [place objectForKey:@"geometry"];
//Get our name and address info for adding to a pin.
NSString *name = [place objectForKey:@"name"];
NSString *vicinity = [place objectForKey:@"vicinity"];
//Get the lat and long for the location.
NSDictionary *loc = [geo objectForKey:@"location"];
//Create a special variable to hold this coordinate info.
CLLocationCoordinate2D restaurantCoord;
//Set the lat and long.
restaurantCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
restaurantCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];
//Create a new annotiation.
MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity coordinate:restaurantCoord];
NSLog(@"mapPoint: %@", placeObject.name);
if ([foundObjects count] > 0 ) {
for (Restaurant *restaurant in foundObjects) {
// NSLog(@"Restaurant: %@", restaurant);
//NSLog(@"mapPoint: %@", placeObject.name);
if (restaurant.restaurantName == placeObject.name) {
return;
} else {
[mapView addAnnotation:placeObject];
}
}
} else {
[mapView addAnnotation:placeObject];
}
}
}
Restaurant
是我的核心数据实体。我正在尝试将餐厅的名称与谷歌查询 api 返回的 mapPoint 的名称进行比较。我尝试 NSLogging 餐厅名称(核心数据)和谷歌 api 餐厅名称的返回值 - 它们似乎匹配。任何建议,或者我是否应该以不同的方式解决这个问题 - 让我知道谢谢!