如何使用 iOS 中的邮政编码获取距离和位置?

How to get distance and location using zipcode in iOS?

我正在开发一个即时消息聊天 app.In,用户必须输入它的邮政编码,当他选择搜索选项时,他会给出一个以英里为单位的特定半径,搜索操作将通过该半径perform.So 现在我有两个邮编,一个是用户的,另一个是我朋友的应该使用 these.Please 帮助我,因为我是 iOS 开发的新手。

参考这个answer to get Latitude and Longitude from zipcode. Once you get the zipcodes of your source and destination, you can get the distance using this answer

请注意,这将是空中距离,而不是实际距离。您可以评估 Google 地图或方向 API 以找到实际距离。

获取 2 个邮政编码之间的距离没有直接的解决方案,但您可以使用 google API 获取邮政编码的经纬度,然后使用这些经纬度可以找到邮政编码之间的距离。

#import <CoreLocation/CoreLocation.h>

 CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];

 CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];

 CLLocationDistance distance = [locA distanceFromLocation:locB];

首先您需要使用 CLGeocoder 将邮政编码/地址转换为 lat/long 位置。获得这些职位后,您可以:

如果您想要实际距离,请使用 CLLocationdistanceFromLocation:

如果您想要行车距离,则需要使用导航框架来获取路线,例如 MKDirections