gpt4 book ai didi

iOS - 从地址获取纬度和经度(地理编码和 Google API)

转载 作者:行者123 更新时间:2023-11-29 00:23:31 24 4
gpt4 key购买 nike

我需要使用 MKPointAnnotation 显示附近人的位置。我只有这些地址。我尝试通过地理编码和 Google API 获取经纬度。有时我会从中得到正确的回应。有时我会收到 NULL 响应和 HTML 响应。我已附上我的代码片段和回复。如何处理 NULL 问题和 HTML 格式的响应。如果您已经遇到过这个问题,请帮助我。以及是否有其他方法可以从地址获取纬度、经度。

方法一:

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);

方法二:

 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>

最佳答案

您收到的 HTML 响应是一个登录页面。

您正在获取登录页面,因为您在没有 API key 的情况下调用 google API。他们允许您在没有 key 的情况下进行一些查询,但在多次查询之后,他们希望您登录,以便他们知道谁在访问他们的 API 和使用他们的资源。

https://developers.google.com/maps/documentation/geocoding/get-api-key

为自己获取一个 API key ,然后在查询中传递它。他们仍然限制您每天查询 2500 次(每秒不超过 50 次查询),除非您有付费/高级帐户,但这应该足以让您开始。

或者按照评论中的建议进行操作并改用 Apple 的地理编码(根据我的经验,Google 提供了更准确的结果,但 Apple 的在大多数情况下就足够了 - 只是准备好处理偶尔出现的结果方式 在您期望的位置之外,例如 Lat=0.0,long=0.0,没有报告错误)。

关于iOS - 从地址获取纬度和经度(地理编码和 Google API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com