本文介绍了来自iPhone / sim的谷歌地理编码API上的状态码0,但在网络上正常工作(非英文字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请尝试: =Őrbottyán,匈牙利& sensor = true

在iPhone 4&模拟器,

   - (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@geocode fail code: %d,[request responseStatusCode]);
NSLog(@geocoding failed:%@,[request responseString]);
}
2011-06-01 11:36:27.343应用[1174:307]地理编码失败代码:0
2011-06-01 11:36:27.345应用[1174:307]地理编码失败:(null)

在浏览器中,我得到:



$ $ p $ results:[
{
address_components:[
{
long_name:Őrbottyán ,
short_name:Őrbottyán,
types:[locality,political]
},
{
long_name: Pest,
short_name:Pest,
types:[administrative_area_level_1,political]
},
{
long_name :匈牙利,
short_name:HU,
types:[country,political]
}
],
formatted_address:Őrbottyán,匈牙利,
geometry:{
bounds:{
northeast:{
lat:47.7138950,
lng:19.34353090
},
southwest:{
lat:47.63339999999999,
lng:19.2051070
}
},
location:{
lat:47.6846190,
lng:19.2883260
},
location_type:APPROXIMATE,
viewport:{
northeast:{
lat:47.7138950,
lng:19.34353090
},
southwest:{
lat:47.63339999999999,
lng:19.2051070
}
}
},
types:[locality,political]
}

],
status:OK
}



其他有标准英文字符的请求可以在设备上正常工作并正常返回。

解决方案

哟你可能使用 NSURL 类和

在将字符串传递给之前,需要使用 [NSString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 将Őrbottyán中的高位字符转换为百分号转义。 code> NSURL 。现代浏览器会在您将不符合规则的字符串放入URL字段时为您进行默认转换,但在代码中您必须明确地执行此操作。






修改以包含以下quantumpotato的调查结果:如果Őrbottyán转换为Orbottyan(有损转换为ASCII编码),则Google地图将做正确的事情,并且可以通过 NSData 往返执行转换:

  NSData * data = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString * dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSURL * url = [NSURL URLWithString:dataString];
[dataString release];

我怀疑有损转换为ASCII可能不适用于所有网站,但它已经过测试和验证与谷歌地图,所以你有它。 : - )

Try: http://maps.googleapis.com/maps/api/geocode/json?address=Őrbottyán,Hungary&sensor=true

On an iPhone 4 & simulator,

 -(void)requestFailed:(ASIHTTPRequest *)request {
   NSLog(@"geocode fail code: %d",[request responseStatusCode]);
   NSLog(@"geocoding failed: %@",[request responseString]);
}
2011-06-01 11:36:27.343 app[1174:307] geocode fail code: 0
2011-06-01 11:36:27.345 app[1174:307] geocoding failed: (null)

In a browser I get:

   "results" : [
    {
    "address_components" : [
        {
           "long_name" : "Őrbottyán",
           "short_name" : "Őrbottyán",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Pest",
           "short_name" : "Pest",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Hungary",
           "short_name" : "HU",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Őrbottyán, Hungary",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 47.7138950,
              "lng" : 19.34353090
           },
           "southwest" : {
              "lat" : 47.63339999999999,
              "lng" : 19.2051070
           }
        },
        "location" : {
           "lat" : 47.6846190,
           "lng" : 19.2883260
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 47.7138950,
              "lng" : 19.34353090
           },
           "southwest" : {
              "lat" : 47.63339999999999,
              "lng" : 19.2051070
           }
        }
     },
     "types" : [ "locality", "political" ]
  }

], "status" : "OK" }

Other requests with standard English characters in them DO work and return correctly on the device.

解决方案

At some point in your code you're probably using the NSURL class, and:

You need to convert the high-bit characters in "Őrbottyán" into percent escapes, using [NSString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], before passing the string to NSURL. Modern browsers will silently make this conversion for you when you put a non-compliant string into the URL field, but in code you have to do it explicitly.


Edit to include quantumpotato's findings below: Google Maps will do the right thing if "Őrbottyán" is converted to "Orbottyan" (a "lossy" conversion to ASCII encoding), and that conversion can be performed with a round-trip through NSData:

NSData *data = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:dataString];
[dataString release];

I suspect "lossy conversion to ASCII" may not work with all websites, but it's been tested and verified with Google Maps, so there you have it. :-)

这篇关于来自iPhone / sim的谷歌地理编码API上的状态码0,但在网络上正常工作(非英文字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:30