gpt4 book ai didi

iphone - 比较 google place API mapPoints 与核心数据对象 mapPoints - mapView

转载 作者:行者123 更新时间:2023-11-29 03:41:55 26 4
gpt4 key购买 nike

在我的 iPhone 应用程序中,我正在利用 google place api - 在 map View 上绘制附近的餐馆。我遇到的问题是当我在同一个 map View 上处理核心数据对象时。澄清一下:用户可以选择一个 pin(餐厅)来加载该餐厅的详细信息屏幕(他们可以在其中保存评论、评级等)-然后我将该餐厅保存为核心数据对象。但是,下次加载 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 是我的核心数据实体。我试图将餐厅的名称与 google 查询 api 返回的 mapPoint 的名称进行比较。我尝试 NSLogging 餐厅名称(核心数据)和 google api 的餐厅名称的返回值 - 它们似乎匹配。任何建议或者我是否应该以不同的方式解决这个问题 - 让我知道谢谢!

最佳答案

问题出在这一行:

if (restaurant.restaurantName == placeObject.name)

这只是将两个指针与字符串进行比较。它总是返回 false。相反,您必须比较字符串是否相等:

if ([restaurant.restaurantName isEqualToString:placeObject.name])

关于iphone - 比较 google place API mapPoints 与核心数据对象 mapPoints - mapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com