【发布时间】:2014-08-15 00:14:42
【问题描述】:
提前感谢您在这里阅读我的问题。我目前正在开发一个使用 mapkit、corelocation 和 google map api (ios) 的地图应用程序。所以这是我的问题:是否可以使用 google map api 返回从结果位置到用户当前位置的距离?我想要实现的效果是制作一个表格视图,其中列出了我的 google map api 搜索结果(医院位置),并按距离顺序(近到远)对它们进行排序。每个单元格中还应该显示一个距离。抱歉,我没有足够的积分来发布照片.. 所以这是我想要达到的效果:Sending API Data to UITableView Within ViewController
到目前为止,我已经设法从地图 api 中提取数据,在地图上放置图钉并将这些搜索结果填充到我的 tableview 中。我已经做了很多搜索,但到目前为止,我还没有看到任何关于使用 google map api 返回距离的文章。
顺便说一句,我真的是 iOS 和谷歌地图 api 的新手。我还注意到有一个谷歌距离矩阵 api,但我不确定如何在 iOS 中使用它。这实际上是我需要用来找出距离的api吗?它似乎没有任何要导入的数据包.. google 的教程不清楚
附:我从地图 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 方法中。 (错误说未声明..)
【问题讨论】:
标签: ios google-maps mapkit core-location