how to get coordinates from the total address of the content?

后端 未结 1 1441
谎友^
谎友^ 2021-01-17 06:15

I am getting total address and coordinates of the location address but I need only coordinates to show on the google map. How is it possible in iPhone?

- (vo         


        
相关标签:
1条回答
  • 2021-01-17 06:35

    Try this

    - (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
    {
       double latitude = 0, longitude = 0;
       NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
       NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
       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;
       return center;
    }
    
    0 讨论(0)
提交回复
热议问题