简体   繁体   中英

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

I need to show nearby person's locations using MKPointAnnotation. I have those addresses only. I tried to get lat, lng over Geocode and Google API. Sometime I get correct response from it. Sometimes I get NULL response and HTML responses. I've attached my code snippet and responses. How will handle NULL issue and HTML Format of response. 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>
<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.

You are getting a login page because you are calling the google API without an API key. 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.

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

Get yourself an API key, then pass it along in the query. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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