iPhone SDK CoreLocation Coordinate Problem

Discussion in 'iOS Programming' started by icinnamon, Aug 17, 2008.

  1. icinnamon macrumors newbie

    Joined:
    Sep 7, 2007
    #1
    I'm using CoreLocation but the latitude and longitude is not returning a negative. For example, my latitude is -118, but corelocation is returning 118.
    I am using newLocation.coordinate.latitude. How do I fix this?
    Thanks!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Can you post the code you are using to get and display this? My first thought is that you are somehow loosing the sign, possibly by store or displaying as an unsigned type...
     
  3. icinnamon thread starter macrumors newbie

    Joined:
    Sep 7, 2007
    #3
    Thanks!
    Here's my code:
    Code:
    -(void)updateLatitude:(NSString *)latitude andLongitude:(NSString *) longitude {
    	latitudeText.text = latitude;
    	longitudeText.text = longitude;
    	
    	
    	loadingText.text = @"Starting...";
    	[[MyCLController sharedInstance].locationManager startUpdatingLocation];
    	NSString *lat = [ latitude stringByAddingPercentEscapesUsingEncoding: 4 ];
    	NSString *lon = [ longitude stringByAddingPercentEscapesUsingEncoding: 4 ];
    	NSString *carrier = [ @"ATT" stringByAddingPercentEscapesUsingEncoding: 4 ];
    	NSString *reception = [ @"7" stringByAddingPercentEscapesUsingEncoding: 4 ];
    	loadingText.text = @"Sending...";
    	NSString *myURL = [ NSString stringWithFormat: @"http://receptionmap.com/uploadForiPhone.php?lat=%@&lon=%@&carrier=%@&reception=%@", lat, lon, carrier, reception ];
    	[ NSData dataWithContentsOfURL: [ NSURL URLWithString: myURL ] ];
    	loadingText.text = @"Uploaded";
    	
    It is not displaying the negative on the screen or in the database (online)
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Call me stupid, but I don't see where you get the current location in that code.

    Oh, and you shouldn't use 4 as your string encoding: use NSUTF8StringEncoding, just incase the number changes in future version of the SDK...
     
  5. icinnamon thread starter macrumors newbie

    Joined:
    Sep 7, 2007
    #5
    Okay I'll do that instead of 4

    And that code is actually the "callback" method (is that what it's called in Objective-C.... I'm used to Python dev :)).

    This is the code that calls that method (this code is in MyCLController.m):
    Code:
    - (void)locationManager:(CLLocationManager *)manager
    	didUpdateToLocation:(CLLocation *)newLocation
    		   fromLocation:(CLLocation *)oldLocation
    {
    	[self.delegate updateLatitude: 
    	 [NSString stringWithFormat:@"%lf", 
    	  fabs(newLocation.coordinate.latitude)] 
    	 andLongitude: 
    	 [NSString stringWithFormat:@"%lf", 
    	  fabs(newLocation.coordinate.longitude)]];
    	
    }
    
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Um... you know what absolute value is, right? =) fabs is taking newLocation.coordinate.latitude and .longitude and forces them to positive, so you are asking to lose the negative with that code.

    -Lee
     
  7. icinnamon thread starter macrumors newbie

    Joined:
    Sep 7, 2007
    #7
    Well that would be a problem, wouldn't it ;)
    Thanks!!!
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    it's always something, huh?

    The only time I can think of with location stuff you'd want absolute value is dealing with distance travelled. You might do something like:
    Code:
    - (void)locationManager:(CLLocationManager *)manager
    	didUpdateToLocation:(CLLocation *)newLocation
    		   fromLocation:(CLLocation *)oldLocation
    {
    	double latitudeDelta = fabs(newLocation.coordinate.latitude - oldLocation.coordinate.latitude);
    	double longitudeDelta = fabs(newLocation.coordinate.longitude - oldLocation.coordinate.longitude);
    	double distance = sqrt(latitudeDelta*latitudeDelta + longitudeDelta*longitudeDelta);
    	[self.delegate setDistanceTravelled: 
    	 [NSString stringWithFormat:@"%lf", 
    	  distance]  
    	];
    }
    
    -Lee
     

Share This Page