我需要使用MKPointAnnotation显示附近人物的位置。我只有这些地址。我试图通过Geocode和Google API获取lat。有时候我得到了正确的答复。有时我得到NULL响应和HTML响应。我已附上我的代码段和回复。如何处理NULL问题和HTML格式的响应。如果您已经遇到过这个问题,请帮助我。如果有任何替代方法可以从地址获取lat,lng。
方法I:
double latitude = 0, longitude = 0;
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=chennai"];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude = longitude;
NSLog(@"Latitute : %f",center.latitude);
NSLog(@"Logitute : %f",center.longitude);
方法II:
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:@"chennai" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"Latitude %f", coordinate.latitude);
NSLog(@"Longitude %f", coordinate.longitude);
}];
HTML回复:
<html>
<head>
<title>Please wait while the login page is loaded...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"/>
<META HTTP-EQUIV="EXPIRES" CONTENT="-1"/>
<META HTTP-EQUIV="Refresh" CONTENT="2;URL=http://201.132.12.642:7002/userportal/?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3dChennai&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0">
</head>
<body>
<p align="center">Please wait...<p>
Please wait while the login page is loaded...
<!---
<msc>
<login_url><![CDATA[http://201.132.12.642:7002/userportal/NSCLOGIN.do?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3dChennai&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0]]></login_url>
<logout_url><![CDATA[http://201.132.12.642:7002/userportal/NSCLOGOUT.do?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3dChennai&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0]]></logout_url>
<status_url><![CDATA[http://201.132.12.642:7002/userportal/NSCSTATUS.do?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3dChennai&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0]]></status_url>
<update_url><![CDATA[http://201.132.12.642:7002/userportal/NSCUPDATE.do?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3dChennai&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0]]></update_url>
<content_url><![CDATA[http://201.132.12.642:7002/userportal/NSCCONTENT.do?requesturi=http%3a%2f%2fmaps%2egoogle%2ecom%2fmaps%2fapi%2fgeocode%2fjson%3fsensor%3dfalse%26address%3dChennai&ip=10%2e0%2e1%2e134&mac=74%3a1b%3ab2%3a29%3a20%3a69&nas=aerovoyce&requestip=maps%2egoogle%2ecom&sc=9f491d49d008fc568408c965a3d3bcf0]]></content_url>
</msc>
-->
</body>
</html>
答案 0 :(得分:1)
您获得的HTML响应是登录页面。
您正在获取登录页面,因为您在没有API密钥的情况下调用Google API。它们允许您在没有密钥的情况下进行一些查询,但经过多次查询后,他们希望您登录,以便他们知道谁在访问其API并使用其资源。
https://developers.google.com/maps/documentation/geocoding/get-api-key
获取一个API密钥,然后在查询中传递它。他们仍然限制您每天2500次查询(每秒不超过50次查询),除非您有付费/高级帐户,但这应该足以让您入门。
或者按照评论中的建议进行操作并使用Apple的地理编码(根据我的经验,Google提供更准确的结果,但Apple在大多数情况下都足够了 - 只是准备好处理偶尔的结果 way 落在您期望的范围之外,例如Lat = 0.0,long = 0.0,没有报告错误。)