我想在我的iPad应用程序中提供用户输入字段,用户可以在其中键入城市或位置的名称,应用程序应显示用户可以选择的可能城市/位置列表,我可以获得地理坐标从(在a上添加标记MKMapView).
我试过了CLGeocoder,但似乎它只给我一个结果,加上结果不准确.例如,将"Stutt"作为地址字符串产生"Straubing,德国"作为结果,wheareas"Stuttgart"将提出"斯图加特"(正确)作为结果(因此我预计"斯图特"至少有2个结果).此外,在美国有一个叫斯图加特的城市,结果也没有给出.(我使用了本页最底部的示例代码:https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html)
任何人都可以根据用户文本输入指导我找到适合城市名称/位置查找的解决方案吗?
我有一个CLGeocoder的问题,当我打电话给geocodeAddressString:withCompletionHandler我时,我只得到一个结果,尽管知道输入的字符串应该返回多个值.类引用甚至指出:
在前向地理编码请求的情况下,如果提供的信息产生多个可能的位置,则可以返回多个地标对象.
但是,我的地标数组只有一个项目:
[geocoderDestination geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error){
NSLog(@"array count:%i", [placemarks count];}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助.
我使用了"Piccadilly,UK","Union Street,UK"之类的字符串,只返回了一个结果.现在我考虑一下,把英国放在最后可能是影响因素.
我想恢复这个和这个问题,因为问题仍然存在,所以我正在写一个新问题.
这是我的代码:
- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block {
self = [super init];
self.operationCompletionBlock = block;
Class cl = NSClassFromString(@"CLGeocoder");
if (cl != nil)
{
if (self.geocoder_5_1 == nil) {
self.geocoder_5_1 = [[cl alloc] init];
}
NSString *address = [parameters objectForKey:kGeocoderAddress];
[self.geocoder_5_1 geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1];
SVPlacemark *placemark;
NSLog(@"placemarks[count] = %i", [placemarks count]);
for (CLPlacemark *mark in placemarks) {
placemark = [[SVPlacemark alloc] initWithPlacemark:mark];
[svplacemarks addObject:placemark];
}
self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error);
}];
}
else …Run Code Online (Sandbox Code Playgroud) 我得到了大约200个地址,我需要知道它们的纬度和经度.我已经创建了一个解析地址的方法,现在我正在尝试使用这些地址获取坐标CLGeocoder.
我目前的方法是并行创建地理编码器,让他们发挥他们的魔力.我注意到他们每个人似乎都采取了一个单独的线程.(所以我在一个点上看到多达100个线程).
我遇到的问题是,在某些时候(大约50个地址之后),地理编码停止返回任何地方标记,并且
NSLog(@"Address not recognized: *%@*",[htc objectForKey:kAddressKey]);
Run Code Online (Sandbox Code Playgroud)
被叫.这是对多个线程或内置CLGeocoder限制的限制吗?可能是因为我没有正确清理地理编码器并需要某种自动释放声明(ARC)吗?
- (void)geocodeArray:(NSMutableArray*)array {
NSMutableDictionary* htc = nil;
objectsToGeocode = array.count;
NSDictionary *htcDictionary =nil;
for (int i = 0; i<array.count;i++) {
htcDictionary = [array objectAtIndex:i];
//create an updated dictionary that would hold the reverse geocoding location
htc = [[NSMutableDictionary alloc] initWithDictionary:htcDictionary];
NSLog(@"geocoding: %@",[htc objectForKey:kAddressKey]);
CLGeocoder* geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:[htc objectForKey:kAddressKey] completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count>0)
{
NSLog(@"Found placemarks for %@",[htc objectForKey:kAddressKey]);
CLPlacemark* placemark = [placemarks objectAtIndex:0]; …Run Code Online (Sandbox Code Playgroud) 我使用CLGeocoder将CLLocation从经度/纬度解码为地名.它工作正常.但是仍然有一件事困扰着我.当我将设备语言设置为英语时,代码的结果如下:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
/* We received the new location */
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
[self.geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* placemarks, NSError* error){
MKPlacemark *placemarker = [placemarks objectAtIndex:0];
NSLog(@"%@",placemarker.locality);
}];
[self.locationManager stopUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
}
以英文显示,例如:chengdu.
当我将设备语言更改为中文时,
placemarker.locality
Run Code Online (Sandbox Code Playgroud)
返回中文字符值.
但我真正想要的是它总会返回一个英文字符值(没有中文字符值).我猜这是与语言环境有关的东西.任何人都可以帮忙吗?谢谢.
我是Objective-C编程的新手.
我想通过邮政编码/邮政编码在MapView中显示位置.为此,我必须找到纬度和经度.
我已经通过将zip代码传递给Web服务并获得所有信息的响应来引用了许多解决方案.
但我不想使用任何网络服务.我想通过使用CLGeocoder来做到这一点
我正在使用下面的代码来查找纬度经度.
-(IBAction)fetchCoordinates:(id)sender {
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
NSString *address = [NSString stringWithFormat:@"%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"mark :: %@", placemarks);
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
self.coordinatesLabel.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude];
if ([placemark.areasOfInterest count] > 0) {
self.nameLabel.text = [placemark.areasOfInterest objectAtIndex:0];
} else {
self.nameLabel.text …Run Code Online (Sandbox Code Playgroud) 使用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.)"
我完全不知道为什么会这样.
当我尝试使用反向地理编码时,出现此错误消息.
地理编码错误:错误域= GEOErrorDomain代码= -3"(null)"
我的代码如下:
import CoreLocation
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
if let placemarks = placemarks {
reverseGeocodeLocations[hash] = placemarks
}
callback(placemarks, error)
}
Run Code Online (Sandbox Code Playgroud)
这只是不时工作,我每秒多次请求reverseGeocode.所以我猜这个错误信息与请求的限制有什么关系?有没有关于apple的地理编码请求的文档?谢谢你提前.
更新
这是我的整个代码请求
import CoreLocation
fileprivate struct ReverseGeocodeRequest {
private static let geocoder = CLGeocoder()
private static var reverseGeocodeLocations = [Int: [CLPlacemark]]()
private static let reverseGeocodeQueue = DispatchQueue(label: "ReverseGeocodeRequest.reverseGeocodeQueue")
private static var nextPriority: UInt = 0
fileprivate static func request(location: CLLocation, callback: @escaping ([CLPlacemark]?, Error?)->Void) {
let hash = location.hash
if let value = reverseGeocodeLocations[hash] …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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,但它没有任何区别 - 我总是收到失败的消息。
其他任何人都遇到过这种情况并找到了解决方案?
clgeocoder ×10
ios ×6
iphone ×3
objective-c ×3
swift ×2
cllocation ×1
geolocation ×1
ios5 ×1
ios7 ×1
ipad ×1
location ×1
nsarray ×1
xcode5 ×1