请提前阅读我的问题。我正在开发一个使用mapkit,corelocation和google map api(ios)的地图应用程序。所以这是我的问题:是否可以使用谷歌地图api返回从结果位置到用户的距离'当前位置?我想要实现的效果是制作一个tableview,列出我的谷歌地图api搜索结果(医院位置),并按距离(接近远)的顺序排序。每个单元格中还应显示距离。对不起,我没有足够的学分来发布图片..所以这是我希望实现的效果:Sending API Data to UITableView Within ViewController
到目前为止,我已设法从地图api中提取数据,将地图上的引脚放入我的tableview中。我做了很多搜索,但到目前为止我还没有看到任何关于使用谷歌地图api返回距离的文章。
顺便说一下,我真的是iOS和谷歌地图api的新手。我还注意到有一个谷歌距离矩阵api,但我不知道如何在iOS中使用它。实际上我需要用api来找出距离吗?它似乎没有任何数据包要导入..谷歌的教程不明确P.S。我从地图api获得的数据是json格式,它包含纬度和经度。我把它们保存在一个数组中
再次感谢您的帮助
嘿伙计们。我已经想出如何计算距离,但我不知道如何显示它。这是我的距离计算代码块enter code here -(void)putPins:(NSArray *)data {
for (id<MKAnnotation> annotation in mapView.annotations) {
if ([annotation isKindOfClass:[MapPins class]]) {
[mapView removeAnnotation:annotation];
}
}
for (int i=0; i<[data count]; i++){
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.
MapPins *placeObject = [[MapPins alloc] initWithName:name address:vicinity coordinate:placeCoord];
[mapView addAnnotation:placeObject];
//test methods
CLLocation *locationA = [[CLLocation alloc]initWithLatitude:currentCentre.latitude longitude:currentCentre.longitude];
CLLocation *locationB = [[CLLocation alloc]initWithLatitude:placeCoord.latitude longitude:placeCoord.longitude];
CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];
NSLog(@"distance in meters=%f",distanceInMeters);
NSString *distanceD = [NSString stringWithFormat:@"%f",distanceInMeters];
}
}
最后一行,NSString * distanceD我试图将distanceInMeters放入NSString格式并以某种方式显示它。但事实证明它根本不起作用。我不能把它放在mappin方法中。 (错误说未申报..)
答案 0 :(得分:0)
因此,由于您拥有要与之比较的点的纬度和经度坐标,并且由于您要与当前位置进行比较,因此您应该可以执行与此问题类似的操作:
Find distance between two points using MKMapKit
您需要做的就是计算单元格中每行的距离,然后每行使用一个CLLocationDistance对象。