Ios googleplacesapi没有返回任何内容
我正试图在MKMapView上显示来自Google Places的结果。调用代码以检索数据,但它不记录任何内容。我认为问题不在于添加注释,因为在日志中,它返回Ios googleplacesapi没有返回任何内容,ios,mkmapview,google-places-api,Ios,Mkmapview,Google Places Api,我正试图在MKMapView上显示来自Google Places的结果。调用代码以检索数据,但它不记录任何内容。我认为问题不在于添加注释,因为在日志中,它返回Google数据:( )没有任何内容。我该如何解决这个问题。这是我正在使用的代码- -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { //Get the east and west points on the map so you
Google数据:(
)
没有任何内容。我该如何解决这个问题。这是我正在使用的代码-
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
MKMapRect mRect = self.schoolMap.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set your current distance instance variable.
currenDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
//Set your current center point on the map instance variable.
currentCentre = self.schoolMap.centerCoordinate;
}
-(void) queryGooglePlaces: (NSString *) googleType {
CLLocation *userLoc = self.schoolMap.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
//Change the 9000!!
// Build the url string to send to Google. NOTE: The kGOOGLE_API_KEY is a constant that should contain your own API key that you 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=%@&types=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType, kGOOGLE_API_KEY];
//Formulate the string as a 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);
[self plotPositions:places];
}
-(void)plotPositions:(NSArray *)data {
// 1 - Remove any existing custom annotations but not the user location blue dot.
for (id<MKAnnotation> annotation in self.schoolMap.annotations) {
if ([annotation isKindOfClass:[MapPoint class]]) {
[self.schoolMap removeAnnotation:annotation];
}
}
// 2 - 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];
// 3 - There is a specific NSDictionary object that gives us the location info.
NSDictionary *geo = [place objectForKey:@"geometry"];
// Get the lat and long for the location.
NSDictionary *loc = [geo objectForKey:@"location"];
// 4 - Get your name and address info for adding to a pin.
NSString *name=[place objectForKey:@"name"];
NSString *vicinity=[place objectForKey:@"vicinity"];
// Create a special variable to hold this coordinate info.
CLLocationCoordinate2D placeCoord;
// Set the lat and long.
placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];
// 5 - Create a new annotation.
MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity coordinate:placeCoord];
[self.schoolMap addAnnotation:placeObject];
}
}
-(void)地图视图:(MKMapView*)地图视图区域IDChangeAnimated:(BOOL)动画{
//获取地图上的东点和西点,以便计算当前地图视图的距离(缩放级别)。
MKMapRect mRect=self.schoolMap.visibleMapRect;
MKMapPoint eastmapoint=MKMapPointMake(MKMapRectGetMinX(mRect)、MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint=MKMapPointMake(MKMapRectGetMaxX(mRect)、MKMapRectGetMidY(mRect));
//设置当前距离实例变量。
CurrendList=各点之间的MkMeters(东映射点、西映射点);
//在地图实例变量上设置当前中心点。
currentCentre=self.schoolMap.centercoordination;
}
-(void)queryGooglePlaces:(NSString*)Google类型{
CLLocation*userLoc=self.schoolMap.userLocation.location;
CLLocationCoordinate2D用户坐标=userLoc.coordinate;
//更改9000!!
//构建要发送到Google的url字符串。注意:kGOOGLE_API_键是一个常量,应该包含您从Google获得的自己的API键。有关详细信息,请参阅此链接:
// 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=%@&types=%@&sensor=true&key=%@,currentcenter.latitude,currentcenter.longitude,[NSString stringWithFormat:@“%i”,currenDist],googleType,kGOOGLE_API_key];
//将字符串表示为URL对象。
NSURL*googleRequestURL=[NSURL URLWithString:url];
//检索URL的结果。
调度异步(kBgQueue^{
NSData*data=[NSData dataWithContentsOfURL:googleRequestURL];
[self-performSelectorOnMainThread:@selector(fetchedData:)with object:data waitUntilDone:YES];
});
}
-(void)获取数据:(NSData*)响应数据{
//解析出json数据
n错误*错误;
NSDictionary*json=[NSJSONSerialization
JSONObjectWithData:responseData
选项:针织品
错误:&错误];
//来自Google的结果将是从NSDictionary对象获得的一个数组,其中包含键“results”。
NSArray*places=[json objectForKey:@“结果”];
//将数据写入控制台。
NSLog(@“谷歌数据:%@”,位置);
[自绘制位置:位置];
}
-(无效)绘图位置:(NSArray*)数据{
//1-删除任何现有的自定义批注,但不删除用户位置蓝点。
for(self.schoolMap.annotations中的id注释){
if([annotation isKindOfClass:[MapPoint class]]){
[self.schoolMap removeAnnotation:annotation];
}
}
//2-遍历从GoogleAPI返回的位置数组。
对于(int i=0;这就是我所说的“无处可去”你能分享你在查询中发送的确切url参数吗?“我正试图在我的MKMapView上显示Google Places的结果”不要这样做。如果你有谷歌数据,你必须在谷歌地图上显示,这是他们的条款和条件。有没有办法在没有谷歌位置的MKMapView上做同样的事情呢?那么@CraigSure,你可以在MKMapView上显示各种结果,你只需要找到一个允许你的源。或者切换到使用谷歌的地图元素