【问题标题】:Google map or matrix api and tableview谷歌地图或矩阵 api 和 tableview
【发布时间】: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


    【解决方案1】:

    因此,由于您有要比较的点的纬度和经度坐标,并且由于您要与当前位置进行比较,因此您应该能够执行类似于此问题的操作:

    Find distance between two points using MKMapKit

    您所要做的就是计算单元格中每一行的距离,然后每行都有一个 CLLocationDistance 对象可以使用。

    【讨论】:

    • 嗨@BHendricks,感谢您的回复。但我的问题是我使用谷歌地图 api url 请求获取 json 文件。所以我每次都必须使用“for key”功能来达到纬度和经度。我很困惑如何将苹果的定位方法与这些 json 数据一起使用。有没有办法让 api 整理出这些距离并在 json 文件中回复我,我可以将它们填充到我的 tableview 中?
    • 我不认为这是可能的,但我可能弄错了。
    • 嘿伙计们,我刚刚发现了一个可能对我有用的方法:CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation];但我在这里的问题是在第一个位置和第二个位置中放入什么。我刚刚将位置名称、lat 和 long 保存到一个数组中。所有数据均来自谷歌。但似乎我需要分别获取 lat 和 long 值,并且.... 说真的,我不知道该怎么做。谢谢你们的帮助
    • 这是我推荐你使用的方法。基本上,对于您获得的每个纬度/经度值,您需要创建一个 CLLocation。然后,您可以使用上述方法生成 CLLocationDistance。
    • 嘿,BHendricks,干杯。但我现在完全不知道该怎么做。你能看看我的代码吗?如果你能给我一个电子邮件地址,我会把它发给你。非常感谢
    猜你喜欢
    • 2017-04-11
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    相关资源
    最近更新 更多