将 mapkit 设置为仅包含城市名称的位置

Set mapkit to location with only a cityname

我从 api 获取数据。此数据仅包含城市名称。 我想向用户展示城市的地图视图。

有人知道怎么做吗? 我发现了一些关于 CLGeocoder api 的信息,这有帮助吗?或者我应该为欧洲的城市找到经纬度的数据库吗?

您将使用 geocodeAddressString 方法通过 CLGeocoder 使用前向地理编码。更多信息,请访问: https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLGeocoder_class/index.html#//apple_ref/occ/instm/CLGeocoder/geocodeAddressString:completionHandler:

或者您也可以点击 Google 地图服务。

您可以使用Google地理编码API获取地址的纬度和经度。

https://maps.googleapis.com/maps/api/geocode/json?address=Newyork&key=PROVIDE_YOUR_API_KEY

其中 API_KEY 您必须在 developer.google.com

中注册

这将 return 一种 JSON 格式,您可以在其中解析并获得最近的纬度和经度。

有关详细信息,请查看此 link https://developers.google.com/maps/documentation/geocoding/

将纬度和经度分配给 CLLocationCoordinate2D 并使用 Markers/Annotations

在地图中添加位置