在iOS中查找手机发射塔位置

在iOS中查找手机发射塔位置,ios,objective-c,json,xcode,networking,Ios,Objective C,Json,Xcode,Networking,现在,我正试图用cellID、MNC、MCC和LAC找到塔的位置。 如果您有cellId、MNC、MCC和LAC,那么您可以在iOS中轻松找到基站位置。经过一番斗争,我得到了这个问题的答案。这就是这个问题的答案。 #define google_geo_location @"https://www.googleapis.com/geolocation/v1/geolocate?key=" 谷歌API位置 NSString * urlString = [NSString stringWithFor

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

这就是这个问题的答案。

#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字符串转换为JSON字典

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"];
    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"];
        }


    }
从字符串创建url

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"];
    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"];
        }


    }
设置请求正文,如果方法类型为“POST”仅使用POST

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"];
    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"];
        }


    }
现在您可以从服务器获得响应了

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"];
    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"];
        }


    }

欢迎您回答自己的问题,但您应该将答案放在“答案”部分,而不是问题本身。从下一次开始,我将执行@paulw11如何获取手机发射塔的详细信息?苹果公司是否为此提供了api?