目标C - 将DMS转换为LAT LONG(南部和东部)

时间:2016-05-24 12:10:29

标签: ios objective-c json coordinates

抬头,说到OBJ-C,我绝对是新手!

我目前正在整理和使用IOS应用程序,并利用我客户端预先加载的数据列表/位置。每个列表/位置的坐标都在DMS中,例如

S:35 00.065
E:148 06.660

有数以千计的商品信息,但我无法为每个商品进行更改。我目前正在通过WordPress JSON API(WP REST API插件)导入该数据。我正在制作的应用程序将JSON数据拉入并相应地填充。

格式简直就是“东方”。和南方的#39;来自JSON URL。

http://example.com/api/get_recent_posts/?custom_fields=name,east,south,image,address,type,Postend&page=%ld

我不知道如何将东方和南方转换为lat并将代码转换为long。除了这种困境,一切都准备好了!

我在这里找到的所有内容都会导致各自教程的过期或死链接,使用Javascript,PHP,或者将LAT LONG LONG转换为DMS。 :(

提前致谢!

--- 编辑可能的答案 ---

感谢@ ronak-chaniyara的回答。只是在实施当前代码时遇到麻烦。来自以下模板的现有代码:

- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *decodedText1 = [_restaurant.name stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
    NSString *decodedText = [decodedText1
                             stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

    self.title = _restaurant.type;
    self.barname.text = decodedText;
    self.address.text = _restaurant.location;

    [[self navigationController] setNavigationBarHidden:NO animated:NO];

    [self loadImageInNewThread];

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"ht7" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    NSString *htmlString2 = [NSString stringWithFormat:htmlString,_restaurant.description];

    self.webView.delegate=self;
    [self.webView loadHTMLString:htmlString2 baseURL:nil];

    _webView.opaque = NO;
    _webView.backgroundColor = [UIColor clearColor];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.306 green:0.306 blue:0.306 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar
     setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    self.EntityPlot.delegate = self;
    CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(_restaurant.lattitude, _restaurant.longditude);
    EntityPlot *coordinate2 =  [[EntityPlot alloc] initWithTitle:decodedText Location:coordinate1];
    [self.EntityPlot addAnnotation: coordinate2];

    CLLocationCoordinate2D annotationCoord;
    annotationCoord.latitude = _restaurant.lattitude;
    annotationCoord.longitude = _restaurant.longditude;

    UIImage *image = [UIImage imageNamed:@"cream_pixels_@2X.png"];
    self.uiView.backgroundColor = [UIColor colorWithPatternImage:image];

    [_barplot setRegion:MKCoordinateRegionMake(annotationCoord, MKCoordinateSpanMake(.005, .005)) animated:YES];
    [_barplot selectAnnotation:coordinate2 animated:YES];

    _barplot.showsUserLocation = YES;

我试图实施:

//Pass string DMS for Lat and Long

- (double)DMSStringToDecimal:(NSString*)strDMS
{

    // split the string
    NSArray *arrSplit = [strDMS componentsSeparatedByString:@":"];

    //direction
    NSString *direction = [arrSplit objectAtIndex:0];

    //degree
    NSString *degreesString = [[[arrSplit objectAtIndex:1] componentsSeparatedByString:@" "] objectAtIndex:0];


    NSArray *arrMinuteandSeconds=[[[[arrSplit objectAtIndex:1] componentsSeparatedByString:@" "] objectAtIndex:1] componentsSeparatedByString:@"."];

    //minutes
    NSString *minutesString = [arrMinuteandSeconds objectAtIndex:0];
    //seconds
    NSString *secondsString = [arrMinuteandSeconds objectAtIndex:1];


    // convert degrees
    double degrees = [degreesString doubleValue];

    // convert minutes
    double minutes = [minutesString doubleValue] / 60;  // 60 degrees in a minute

    // convert seconds
    double seconds = [secondsString doubleValue] / 3600; // 60 seconds in a minute, or 3600 in a degree

    // add them all together
    double decimal = degrees + minutes + seconds;

    // determine if this is negative. south and west would be negative values
    if ([direction.uppercaseString isEqualToString:@"W"] || [direction.uppercaseString isEqualToString:@"S"])
    {
        decimal = -decimal;
    }

    return decimal;
}


#pragma mark - View lifecycle

- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *decodedText1 = [_restaurant.name stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
    NSString *decodedText = [decodedText1
                             stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

    self.title = _restaurant.type;
    self.barname.text = decodedText;
    self.address.text = _restaurant.location;

    [[self navigationController] setNavigationBarHidden:NO animated:NO];

    [self loadImageInNewThread];

    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"ht7" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    NSString *htmlString2 = [NSString stringWithFormat:htmlString,_restaurant.description];

    self.webView.delegate=self;
    [self.webView loadHTMLString:htmlString2 baseURL:nil];

    _webView.opaque = NO;
    _webView.backgroundColor = [UIColor clearColor];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.306 green:0.306 blue:0.306 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar
     setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    self.EntityPlot.delegate = self;


    // location crap

    NSString *latStr = [NSString stringWithFormat:htmlString,_restaurant.lattitude];
    NSString *lonStr = [NSString stringWithFormat:htmlString,_restaurant.longditude];

    double lat = [self DMSStringToDecimal:latStr]; //which gives lat=-35.018055555555556
    double lon = [self DMSStringToDecimal:lonStr]; //which gives lon=148.28333333333333

    // CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

    CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(lat, lon);
    EntityPlot *coordinate2 =  [[EntityPlot alloc] initWithTitle:decodedText Location:coordinate1];
    // CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(_restaurant.lattitude, _restaurant.longditude);
    // EntityPlot *coordinate2 =  [[EntityPlot alloc] initWithTitle:decodedText Location:coordinate1];

    [self.EntityPlot addAnnotation: coordinate2];

    CLLocationCoordinate2D annotationCoord;
    annotationCoord.latitude = lat;
    annotationCoord.longitude = lon;
    // annotationCoord.latitude = _restaurant.lattitude;
    // annotationCoord.longitude = _restaurant.longditude;




    UIImage *image = [UIImage imageNamed:@"cream_pixels_@2X.png"];
    self.uiView.backgroundColor = [UIColor colorWithPatternImage:image];

    [_barplot setRegion:MKCoordinateRegionMake(annotationCoord, MKCoordinateSpanMake(.005, .005)) animated:YES];
    [_barplot selectAnnotation:coordinate2 animated:YES];

    _barplot.showsUserLocation = YES;

}

1 个答案:

答案 0 :(得分:0)

可能你可以做下面的事情,(来自答案:https://stackoverflow.com/a/28545279/5575752):

NSString *latStr=@"S:35 00.065";
NSString *lonStr=@"E:148 06.660";

double lat = [self DMSStringToDecimal:latStr]; //which gives lat=-35.018055555555556
double lon = [self DMSStringToDecimal:lonStr]; //which gives lon=148.28333333333333

// Now create Location object
CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lon];


//Pass string DMS for Lat and Long

- (double)DMSStringToDecimal:(NSString*)strDMS
{

    // split the string
    NSArray *arrSplit = [strDMS componentsSeparatedByString:@":"];

    //direction
    NSString *direction = [arrSplit objectAtIndex:0];

    //degree
    NSString *degreesString = [[[arrSplit objectAtIndex:1] componentsSeparatedByString:@" "] objectAtIndex:0];


    NSArray *arrMinuteandSeconds=[[[[arrSplit objectAtIndex:1] componentsSeparatedByString:@" "] objectAtIndex:1] componentsSeparatedByString:@"."];

    //minutes
    NSString *minutesString = [arrMinuteandSeconds objectAtIndex:0];
    //seconds
    NSString *secondsString = [arrMinuteandSeconds objectAtIndex:1];


    // convert degrees
    double degrees = [degreesString doubleValue];

    // convert minutes
    double minutes = [minutesString doubleValue] / 60;  // 60 degrees in a minute

    // convert seconds
    double seconds = [secondsString doubleValue] / 3600; // 60 seconds in a minute, or 3600 in a degree

    // add them all together
    double decimal = degrees + minutes + seconds;

    // determine if this is negative. south and west would be negative values
    if ([direction.uppercaseString isEqualToString:@"W"] || [direction.uppercaseString isEqualToString:@"S"])
    {
        decimal = -decimal;
    }

    return decimal;
}

根据您的要求进行修改。

希望它会有所帮助:)