when i try to get the current location using
CLLocationCoordinate2D CurrentLocation;
CurrentLocation = map.userLocation.coordinate;
i get the follwoing values:
CurrentLocation.latitude= -180.000000;
CurrentLocation.longitude= -180.000000;
and it is not my location at all.
do you know what is missing in this?
Firstly, make sure that you have set the mapView's showUserLocation to TRUE.
Secondly, check that the value is not nil, as the mapView needs to locate the user first, and that takes a few seconds.
Related
I was doing gps location by using CLLocation. When i press submit button and start the process to get the coordinate. However the locationManager being called multiple times.
How can I get the value from last called in locationManager function ?
Thanks
locationManager.location will give you the most recently retrieved location if it exists, otherwise the value will be nil.
I want to set center of RMMapView with position that's _tempCoordinated2D (CLLocationCoordinate2D)
Normally, It should be
// _rmMapView is RMMapView Class
[_rmMapView setCenterCoordinate:_tempCoordinated2D];
// I try [_rmMapView setCenterCoordinate:_tempCoordinated2D animated:YES];
// I try [_rmMapView setCenterCoordinate:_tempCoordinated2D animated:NO];
Result's wrong.
Because Center of the rmMapView move to _tempCoordinated2D and move back to Current Location.
I have checked my code. It doesn't have a code that's setCenterCoordinate to Current Location.
The problem is not just that.
BUT This's a problem hasn't occurred ,
If I touch the map and move map before run statement setCenterCoordinate of RMMapView.
I don't know why.
Sorry for my bad English.
Do you have a userTrackingMode set on the RMMapView that is causing it to center on the user? Try setting that to RMUserTrackingModeNone.
Hey I am trying to get distance from my current location to another location. But while trying to do so I have been stuck for last 5 hrs. Heres what I am doing and am unable to figure why am I getting an error here:
CLLocation *myloc=[[CLLocation alloc]initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
CLLocationDistance dince = [myloc distanceFromLocation:location];
I have searched it all over the net and everyone is using the same lines, but to me it says incompatible type for argument. Can someone please tell me what is wrong here?
location is of the type CLLocationCordinate2D.
For the benefit of future readers, I'll point out that the problem was that -distanceFromLocation: expects its parameter to be a CLLocation*, not a CLLocationCoordinate2D.
Shouldn't there be a * in the second line? Like this? -
CLLocationDistance *dince = [myloc distanceFromLocation:location];
I am wondering if there is some way to make it so CLLocationManager doesn't automatically returned a cached location. I understand that the documents say "The location service returns an initial location as quickly as possible, returning cached information when available" but this cached location could be extremely far away from the user's current location and I would like it more precise if possible.
Thanks for any help!
You can't stop it from caching, but it's not hard to filter out cached data. Core Location includes a timestamp with its locations. Compare the timestamp of the location with a timestamp saved when your app started, and you'll be able to tell which locations are old (cached, found before your app stated) and which are new. Throw away the old ones.
The location timestamp is an NSDate, so just get the value of [NSDate date] when your app starts up and use that as your reference point when filtering locations. You could even throw away the reference value once you start getting new data and treat a nil reference date as implying that new locations should be trusted.
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
if ([newLocation.timestamp timeIntervalSinceNow] > -10.0) // The value is not older than 10 sec.
{
// do something
}
}
You can use this property of CLLocationManager:
#property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocation *location;
The value of this property is nil if no location data has ever been retrieved, otherwise, this is where CoreLocation caches its data. Therefore, if you always want to start from scratch, simply check if this property is nil or not. If it's nil, then you are ok; otherwise, you need to stop your location manager and start it again: this will force an update, which will result in the cached value being overwritten by the fresh one you have just triggered.
I need to simulate how my application will look when a user is driving around for a demo. I have a MKMapView, how can I simulate the look of a user driving around which will use the map.userLocation functionality, which obviously will not be available in the demo.
Thanks!
No way to simulate in iPhone simulator. You'll need to load it onto your device and move around.
Well I got something going, I just did essentially this
- (void)moveIcon:(MKAnnotationView*)locationView toLocation:(CLLocation*)newLoc
{
LocationAnnotation* annotation = [[[LocationAnnotation alloc] initWithCoordinate:newLoc.coordinate] autorelease];
[locationView setAnnotation:annotation];
[map setCenterCoordinate:newLoc.coordinate animated:YES];
}
Then I call this guy in a loop between all of my vertices with a slight delay. Works quite qell.
I'm not an iPhone dev expert, but how does the map view receive the coordinates? If it's through a function that calls the CoreLocation API, could you possibly just write a function that randomly generates longitude and latitude values at a certain time interval and have your map view pull the coordinates from there instead? Just a thought.
You could also check out iSimulate which claims to be able to simulate several features only available on the iPhone in the iPhone simulator include CoreLocation. I have not tried this myself so your mileage may vary.
In order to simulate driving you'll need to establish 2 basic functionalities:
Reading CLLocations from an archive (which you'd log during the drive test with a device). Ideally you'll do this based on the timestamps on the locations, i.e. reproducing the exact same location updates which were received during the drive test.
Updating your MKAnnotationView's position on the map based on the locations read from log.
For part 1, take a look at CLLocationDispatch, a handy class which provides archiving/unarchiving of CLLocations and dispatches them to one or more listeners (using CLLocationManagerDelegate protocol).
For part 2, take a look at Moving-MKAnnotationView.
I found a better way would be to subclass MKUserLocation:
class SimulatedUserLocation: MKUserLocation {
private var simulatedCoordinate = CLLocationCoordinate2D(latitude: 39, longitude: -76)
override dynamic var coordinate: CLLocationCoordinate2D {
get {
return simulatedCoordinate
}
set {
simulatedCoordinate = newValue
}
}
}
Then add it as an annotation mapView.addAnnotation(SimulatedUserLocation()). (You might also want to hide the real location first mapView.showsUserLocation = false)
iOS would render the annotation exactly like the real user location.
dynamic is used on the property so that changing coordinate triggers KVO and moves it on the map.
The answer is NO. Then, how about adding an abstraction layer between your code and MKMapKit? You can do xUnit tests for your objective.