【问题标题】:find cell tower location in iOS在 iOS 中查找手机信号塔位置
【发布时间】:2016-02-17 08:34:08
【问题描述】:

现在我正在尝试使用 cellID、MNC、MCC 和 LAC 来查找塔的位置。 如果您有 cellId、MNC、MCC 和 LAC,那么您可以轻松找到 iOS 中的基站位置。经过一番挣扎,我得到了这个问题的答案。

【问题讨论】:

  • 欢迎您回答自己的问题,但您应该将答案放在“答案”部分,而不是问题本身。
  • 下次我会做@Paulw11

标签: ios objective-c json xcode networking


【解决方案1】:

现在这是这个问题的答案。

#define google_geo_location @"https://www.googleapis.com/geolocation/v1/geolocate?key="

谷歌 API 位置

NSString * urlString = [NSString stringWithFormat:@"google API key ",google_geo_location];

创建 JSON 字符串

NSString *json=[NSString stringWithFormat:@"[{\"homeMobileCountryCode\": \"%@\",\"homeMobileNetworkCode\": \"%@\",\"cellTowers\": [{\"cellId\": \"%@\",\"locationAreaCode\": \"%@\",\"mobileCountryCode\": \"%@\",\"mobileNetworkCode\": \"%@\" }]}]",_mcctext,_mnctext,_cellidtext,_lactext,_mcctext,_mnctext];

JSON STRING 转换为 JSON DICTIONARY

NSError *jsonError;
 NSData *objectData = [json dataUsingEncoding:NSUTF8StringEncoding];
 NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];

从字符串创建网址

NSURL *urlStr=[NSURL URLWithString:urlString];
    if (urlStr == nil)
    {urlStr = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    }
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:urlStr];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

设置请求正文,如果方法类型是“POST”只使用POST

    NSData *data=[NSJSONSerialization dataWithJSONObject:jsondic options:NSUTF8StringEncoding error:nil];
NSString* jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"jsonString.....%@",jsonString);
    NSData *requestBody = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

// for set requestBody
[request setHTTPBody:requestBody];

现在你得到了服务器的响应

NSHTTPURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

    NSMutableDictionary *resultantDict;

    if (responseData != nil)
    {
        resultantDict=[NSJSONSerialization
                       JSONObjectWithData:responseData
                       options:NSJSONReadingMutableLeaves
                       error:nil];
        NSLog(@"resultantDict=%@",resultantDict);


        NSString *errorCode=@"";
        NSString *errorMessage=@"";
        if ([[resultantDict allKeys] containsObject:@"error"])
        {
            errorCode=[NSString stringWithFormat:@"%@", [[resultantDict valueForKey:@"error"]valueForKey:@"code"]];
            errorMessage= [[resultantDict valueForKey:@"error"]valueForKey:@"message"];
        }


    }

【讨论】:

  • 如何获取手机信号塔的详细信息?苹果是否为此提供了任何api?
猜你喜欢
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多