我正在使用 google maps places autocomplete API 来自动完成用户输入的地址,一旦我完成了所需地址的选择,将选定的地址字符串从 google places 自动完成的地址传递到 google maps geocode api,以获得详细的组件,问题是有时 geocode API 不返回街道号码,即使自动完成 API 返回的相同地址有它,也不确定哪里出错了,下面是我使用的代码和两个服务的输出
这就是我从地理编码服务中检索详细地址的方式
NSString* address = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",completionText];
NSString* str = [address stringByReplacingOccurrencesOfString:@","
withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" "
withString:@"+"];
NSURL *requestURL = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];
//response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSLog(@"%@",locationResults);
NSDictionary* results = [[locationResults objectForKey:@"results"] objectAtIndex:0];
NSArray* address_components = [results objectForKey:@"address_components"];
for (id addr in address_components) {
NSString *long_name = [addr valueForKey:@"long_name"];
NSString *short_name = [addr valueForKey:@"short_name"];
NSString *types = [[addr valueForKey:@"types"] objectAtIndex:0];
if ([types rangeOfString:@"street_number"].location != NSNotFound)
{
streetNum = long_name;
}
else if ([types rangeOfString:@"route"].location != NSNotFound)
{
streetName = long_name;
}
else if ([types rangeOfString:@"locality"].location != NSNotFound)
{
suburb = long_name;
}
else if ([types rangeOfString:@"administrative_area_level_1"].location != NSNotFound)
{
state = short_name;
}
else if ([types rangeOfString:@"country"].location != NSNotFound)
{
country = short_name;
}
else if ([types rangeOfString:@"postal_code"].location != NSNotFound)
{
postalCode = short_name;
}
这是一个地址示例,其中地址具有街道号码但地理编码点返回它
{
"description" : "740 Spring Lane, Wallacedale, Victoria, Australia",
"id" : "ad2c41d2f934a30f4c5f8c4b336df8de988174d6",
"matched_substrings" : [
{
"length" : 10,
"offset" : 0
},
{
"length" : 8,
"offset" : 30
},
{
"length" : 9,
"offset" : 40
}
],
"place_id" : "EjE3NDAgU3ByaW5nIExhbmUsIFdhbGxhY2VkYWxlLCBWaWN0b3JpYSwgQXVzdHJhbGlh",
"reference" : "CkQ1AAAASPpKC9JU4XQoFf3LUYwoF_hJFRpFt5VE67NhCPs8vV9ghXvVUeKgPm-BH-Owx-zvuhHEPhJU-Y2ReK5S49AdphIQiKpvir0TegpxEAjmmNJEJhoUCt7KXyZ6LoN51qT50Unrl5ceYMU",
"terms" : [
{
"offset" : 0,
"value" : "740 Spring Lane"
},
{
"offset" : 17,
"value" : "Wallacedale"
},
{
"offset" : 30,
"value" : "Victoria"
},
{
"offset" : 40,
"value" : "Australia"
}
],
"types" : [ "route", "geocode" ]
}
以下是上述自动完成地址的 Google map 地理编码 API 的输出
{
results = (
{
"address_components" = (
{
"long_name" = "Spring Lane";
"short_name" = "Spring Ln";
types = (
route
);
},
{
"long_name" = Wallacedale;
"short_name" = Wallacedale;
types = (
locality,
political
);
},
{
"long_name" = Victoria;
"short_name" = VIC;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Australia;
"short_name" = AU;
types = (
country,
political
);
},
{
"long_name" = 3303;
"short_name" = 3303;
types = (
"postal_code"
);
}
);
"formatted_address" = "Spring Lane, Wallacedale VIC 3303, Australia";
geometry = {
bounds = {
northeast = {
lat = "-37.9132082";
lng = "141.8506572";
};
southwest = {
lat = "-37.9283224";
lng = "141.8505337";
};
};
location = {
lat = "-37.9266631";
lng = "141.8506572";
};
"location_type" = "GEOMETRIC_CENTER";
viewport = {
northeast = {
lat = "-37.9132082";
lng = "141.8519444302915";
};
southwest = {
lat = "-37.9283224";
lng = "141.8492464697085";
};
};
};
"partial_match" = 1;
"place_id" = "ChIJV74lHNPCzGoRnS8t_EDR8zk";
types = (
route
);
}
);
status = OK;
}
输出没有组件类型“街道号码”,其中自动完成的地址字符串在开头。很多地址都会出现这种情况,有什么问题吗?或者我需要使用其他服务吗? 具有街道号码的地址示例如下
这是由自动完成 API 返回的
{
"description" : "11 Victoria Parade, Collingwood, Victoria, Australia",
"id" : "dbeebdb81c8babd166ac8f89878e88c2cba333a1",
"matched_substrings" : [
{
"length" : 18,
"offset" : 0
},
{
"length" : 9,
"offset" : 43
}
],
"place_id" : "EjQxMSBWaWN0b3JpYSBQYXJhZGUsIENvbGxpbmd3b29kLCBWaWN0b3JpYSwgQXVzdHJhbGlh",
"reference" : "CkQ4AAAATy4Z7xc3ermqeWnlxpl81pAVsBWOIl3S2JbCwwRxraLt39OJauWFnCR3NgGyruVBXfUo236mMe6CCQt5XRSYwRIQEL8JC7Qo8j3-0iD4AnJTtBoURjzgm8aHBa2g0tY4lkTKYTuVGPg",
"terms" : [
{
"offset" : 0,
"value" : "11 Victoria Parade"
},
{
"offset" : 20,
"value" : "Collingwood"
},
{
"offset" : 33,
"value" : "Victoria"
},
{
"offset" : 43,
"value" : "Australia"
}
],
"types" : [ "route", "geocode" ]
}
下面是 Geocode 服务在将自动完成的地址作为输入发送后返回的结果
results = (
{
"address_components" = (
{
"long_name" = 11;
"short_name" = 11;
types = (
"street_number"
);
},
{
"long_name" = "Victoria Parade";
"short_name" = "Victoria Parade";
types = (
route
);
},
{
"long_name" = Collingwood;
"short_name" = Collingwood;
types = (
locality,
political
);
},
{
"long_name" = Victoria;
"short_name" = VIC;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = Australia;
"short_name" = AU;
types = (
country,
political
);
},
{
"long_name" = 3066;
"short_name" = 3066;
types = (
"postal_code"
);
}
);
"formatted_address" = "11 Victoria Parade, Collingwood VIC 3066, Australia";
geometry = {
bounds = {
northeast = {
lat = "-37.8087633";
lng = "144.9830508";
};
southwest = {
lat = "-37.8087778";
lng = "144.9830487";
};
};
location = {
lat = "-37.8087633";
lng = "144.9830508";
};
"location_type" = "RANGE_INTERPOLATED";
viewport = {
northeast = {
lat = "-37.8074215697085";
lng = "144.9843987302915";
};
southwest = {
lat = "-37.8101195302915";
lng = "144.9817007697085";
};
};
};
"place_id" = EjMxMSBWaWN0b3JpYSBQYXJhZGUsIENvbGxpbmd3b29kIFZJQyAzMDY2LCBBdXN0cmFsaWE;
types = (
"street_address"
);
}
);
status = OK;
}
我们将不胜感激任何帮助。
最佳答案
你在自动完成结果的描述中得到的只是一个字符串,仅此而已(不是一组特征,如街道、 zip 或门牌号)。该服务尝试找到确切的位置(基于输入),但匹配可能是部分的。
当你例如在 http://maps.google.com 输入 740 Spring Lane, Wallacedale, Victoria, Australia你会得到 Spring Ln, Wallacedale VIC 3303,Australia
的结果您也可以输入 12345 Spring Lane, Wallacedale, Victoria, Australia ,您会得到相同的结果。
除此之外:我建议 request the place-details基于 place_id 。虽然它一定不能给你想要的结果(一个门牌号),但结果应该更准确(绑定(bind)到一个特定的地方而不是一个地方的地址)。它还将返回该地点的更多详细信息。
关于ios - Google map 地址自动完成和地理编码 API 返回差异地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689241/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha