簡體   English   中英

iOS-從地址獲取經緯度(地理編碼和Google API)

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

我需要使用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響應是一個登錄頁面。

之所以會獲得登錄頁面,是因為您在調用Google API時沒有API密鑰。 他們允許您進行一些不帶鍵的查詢,但經過多次查詢后,他們希望您登錄,以便他們知道誰在訪問其API和使用其資源。

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

為自己獲取一個API密鑰,然后將其傳遞給查詢。 除非您有付費/高級帳戶,否則它們仍然將您每天限制為2500個查詢(每秒不超過50個查詢),但這足以讓您入門。

還是作為意見提出,並使用蘋果的地理編碼,而不是(在我的經驗,谷歌提供了更精確的結果,但蘋果在大多數情況下足夠的-剛剛准備處理的偶然結果落在方式之外,你想到哪里的,如Lat = 0.0,long = 0.0,未報告錯誤)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM