简体   繁体   English

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

[英]iOS - Get Latitude and Longitude from Address (Geocode and Google API)

I need to show nearby person's locations using MKPointAnnotation. 我需要使用MKPointAnnotation显示附近人的位置。 I have those addresses only. 我只有那些地址。 I tried to get lat, lng over Geocode and Google API. 我试图通过地理编码和Google API获得帮助。 Sometime I get correct response from it. 有时我会从中得到正确的回应。 Sometimes I get NULL response and HTML responses. 有时我会得到NULL响应和HTML响应。 I've attached my code snippet and responses. 我已附上我的代码段和响应。 How will handle NULL issue and HTML Format of response. 如何处理NULL问题和HTML格式的响应。 Please help me if you had faced this issue already. 如果您已经遇到此问题,请帮助我。 and if any alternative way to get lat,lng from address. 以及是否有其他方法可以从地址获取经纬度。

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

METHOD 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 RESPONSE: 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>

The HTML response you are getting is a login page. 您得到的HTML响应是一个登录页面。

You are getting a login page because you are calling the google API without an API key. 之所以会获得登录页面,是因为您在调用Google API时没有API密钥。 They let you do a few queries without a key, but after more than a few they want you to log in so they know who is accessing their API and using their resources. 他们允许您进行一些不带键的查询,但经过多次查询后,他们希望您登录,以便他们知道谁在访问其API和使用其资源。

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

Get yourself an API key, then pass it along in the query. 为自己获取一个API密钥,然后将其传递给查询。 They still limit you to 2500 queries per day (and no more than 50 queries per second) unless you have a paid/premium account, but that should be enough to get you started. 除非您有付费/高级帐户,否则它们仍然将您每天限制为2500个查询(每秒不超过50个查询),但这足以让您入门。

Or do as suggested in the comments and use Apple's geocoding instead (in my experience, Google provides more accurate results, but Apple's are adequate in most cases - just be prepared to handle the occasional result that falls way outside of where you expect, such as Lat=0.0, long=0.0, with no error reported). 还是作为意见提出,并使用苹果的地理编码,而不是(在我的经验,谷歌提供了更精确的结果,但苹果在大多数情况下足够的-刚刚准备处理的偶然结果落在方式之外,你想到哪里的,如Lat = 0.0,long = 0.0,未报告错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM