使用CLLocation对象(内容例如纬度= 48.196169,经度= 11.620237),我尝试获取当前的城市和国家:
if(!geocoder) {
geocoder = [[CLGeocoder alloc] init];
}
if (geocoder.geocoding) [geocoder cancelGeocode];
[geocoder reverseGeocodeLocation:lo completionHandler:^(NSArray *placemarks, NSError *error) {
if(devMode) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
}
if (error == nil && [placemarks count] > 0) {
// MY CODE - here placemarks is always (null)
} else {
if(devMode)
NSLog(@"%@", error.debugDescription);
}
}];
Run Code Online (Sandbox Code Playgroud)
大多数情况下,这很有效.但在极少数情况下,我只是得到错误:
PBRequester失败,错误错误域= NSURLErrorDomain代码= -1000"UngültigeURL"UserInfo = 0x16f5ff30 {NSUnderlyingError = 0x16f57810"UngültigeURL",NSLocalizedDescription =UngültigeURL}
找到地标:(null),错误:错误Domain = kCLErrorDomain Code = 2"操作无法完成.(kCLErrorDomain error 2.)"
我完全不知道为什么会这样.
我正在尝试使用CLGeocoder().reverseGeocodeLocation.
它给了我国家名称、街道名称、州和许多其他东西,但没有城市。我的代码有什么问题吗?
这是我的代码:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
CLGeocoder().reverseGeocodeLocation(location) { (placeMark, error) in
if error != nil{
print("Some errors: \(String(describing: error?.localizedDescription))")
}else{
if let place = placeMark?[0]{
print("country: \(place.administrativeArea)")
self.lblCurrentLocation.text = place.administrativeArea
}
}
} }
Run Code Online (Sandbox Code Playgroud)
我也使用下面的代码。但对我不起作用。这是另一种方式。
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Address dictionary
print(placeMark.addressDictionary …Run Code Online (Sandbox Code Playgroud) 每当我打电话时:
reverseGeocodeLocation(_ location: CLLocation, preferredLocale locale: Locale?, completionHandler: @escaping CLGeocodeCompletionHandler)
Run Code Online (Sandbox Code Playgroud)
我进入控制台:
[GEOAddressObject] [NSLocale currentLocale] failed for NSLocaleCountryCode@
Run Code Online (Sandbox Code Playgroud)
我在iOS 13 模拟器(iPhone 6s)上得到了这个,所以我不知道这是否也发生在真实设备上。我已经测试了nil和 any Localefor preferredLocale,但它没有任何区别 - 我总是收到失败的消息。
其他任何人都遇到过这种情况并找到了解决方案?
我正在 iOS 8.0 中开发应用程序。
通过下面的链接,我正在分配位置对象, 如何从 iOS 中的用户获取当前位置
我的目标是按当前位置查找城市、州和国家/地区名称。使用此链接的答案,我得到了苹果地理编码器中使用的地点标记对象。 从 CLGeocoder 获取当前城市和国家/地区?
我一切都好,只是
当我获取美国国家的州名时。它为我提供了短代码,而不是为我提供完整的州名称。
例如 因为Alaska它给了我AK
因为Alabama它给了我 AL
就像这是我的日志,
North Kingstown, RI, United States
Run Code Online (Sandbox Code Playgroud)
我想要完整的州名称,因为我想在我的项目中使用完整的州名称。
我通过这段代码获取州名,
NSString *stateName=myPlacemark.administrativeArea;
Run Code Online (Sandbox Code Playgroud) 为什么CLGeocoder reverseGeocodeLocation在查找地址时会返回带有不同纬度/经度的地标?
背景:根据用户在地图上“长按”以放置图钉,但是我的代码反向进行地理编码,以便能够在图钉上放置区域名称,但随后放置的图钉位于其他位置(即良好的用户体验)。因此,我正在寻找一种方法来利用用户实际选择的经度/纬度,但是只需查找位置名称即可。
示例:通过下面的代码,我看到:
码:
import UIKit
import CoreLocation
import PlaygroundSupport
// Initial Test Co-ordinate
let locInput = CLLocation(latitude: -28.778021889561444, longitude: 152.97857401126666)
print("Input: \(locInput.coordinate.latitude), \(locInput.coordinate.longitude)")
// Initiate Reverse Geocoding
let geocoder: CLGeocoder = CLGeocoder()
print("About to call reverse geocoding function")
geocoder.reverseGeocodeLocation(locInput) { placemarks, error in
guard let placemarks = placemarks else {fatalError("No Placemarks Provided \(error?.localizedDescription)")}
for p in placemarks {
print("OUTPUT: \(p.location!.coordinate.latitude), \(p.location!.coordinate.longitude)")
}
}
// Enable Asynch
PlaygroundPage.current.needsIndefiniteExecution = true
Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试使用 geocodeAddressString 将地址转换为地图上的地标,但是无论我传递给此方法的地址是什么,下面的块都永远不会被执行,有人能给我一些启发吗?非常感谢顺便说一句,我可以在控制台上看到“之前”和“之后”,但没有“你好”
let geo = CLGeocoder()
print("before")
geo.geocodeAddressString(("4 Bradford St, Perth WA 6050"), completionHandler: {
(placemarks, error) -> Void in
print ("hello")
if let placemark = placemarks?[0]
{
self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
}
})
print ("after")
Run Code Online (Sandbox Code Playgroud) 有关如何在Apple文档旁边使用正向地理编码的工作示例或指导的说明.多数民众赞成在一般(我不明白)
请这太好了!也有人知道他们是否使用Google API实现相同或自己的?
鉴于经度和纬度不是我当前的位置,我如何使用GLGeocoder?执行反向地理编码查找?
self.geoCoder = [[CLGeocoder alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// [self.locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: ^(NSArray *placemarks, NSError *error) {
// [self.locationManager stopUpdatingLocation];
CLPlacemark *placemark = [placemarks objectAtIndex:0];
// Long address
// NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// Short address
NSString *locatedAt = [placemark subLocality];
cell.textLabel.text = spot.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"Cab no: %@, spotted at %@", spot.cabno, locatedAt];
}];
Run Code Online (Sandbox Code Playgroud)
显然只会对我的位置进行地理编码,但我需要明确设置经度和纬度来反转.
我正在尝试将字符串地理编码为快速操场中的纬度/经度.这是我的代码:
import CoreLocation
var geocoder = CLGeocoder()
geocoder.geocodeAddressString("San Francisco, CA", {(placemarks, error)->Void in
println("here")
})
Run Code Online (Sandbox Code Playgroud)
但是,here永远不会打印到控制台(控制台输出为空白).为什么?
表明有一些变化
func geocodeAddressString(_ addressString: String!, completionHandler completionHandler: CLGeocodeCompletionHandler!)
Run Code Online (Sandbox Code Playgroud)
至:
func geocodeAddressString(_ addressString: String, completionHandler completionHandler: CLGeocodeCompletionHandler)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
var geocoder = CLGeocoder()
geocoder.geocodeAddressString("\(event!.street), \(event!.city), \(event!.country)", completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
self.event!.lat = placemark.location!.coordinate.latitude
self.event!.long = placemark.location!.coordinate.longitude
self.event!.getMiles(self.currentLocation!.location!.coordinate.latitude, clong: self.currentLocation!.location!.coordinate.longitude)
var mile = self.event!.miles != nil ? NSString(format: "%.1f miles", self.event!.miles!) : "Location services off"
self.milesButton.setTitle(mile as String, forState: .Normal)
}
})
Run Code Online (Sandbox Code Playgroud)
尝试:
var geocoder = CLGeocoder()
let address = …Run Code Online (Sandbox Code Playgroud) clgeocoder ×10
ios ×6
swift ×5
iphone ×2
objective-c ×2
xcode ×2
cllocation ×1
cocoa ×1
geocoding ×1
ios7 ×1
maps ×1
mkmapview ×1
xcode5 ×1