I am making google map integration in iphone. I have to show current location showing in map. How will i get current location ?
Thanks
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
Search for Accessing the Device’s Current Location in following article:
MKMapView Class Reference
showsUserLocation (MapView.showsUserLocation = YES;)
userLocationVisible
userLocation
Related SO posts:
iPhone current user location coordinates showing as (0,0)
How to view the current location in google map using longitude and latitude of that location in Objective C
I think the question has been answered -
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
place this in the viewdidappear:animated
then you should find that you have the right location. Doing it any sooner and the location won't be set yet and you'll probably end up in the ocean :)
The short answer is to write code like mapview.showsUserLocation = YES;, where mapview is your UIMapView class object.
Related
Ive got an app that plots mkannotations (i hope i get my terminology right...its kinda confusing) on a mapview.
I have already included the subtitle for when you tap on them.
I have been looking online for a way to include the distance in those callouts but im not quite there yet. I ran across two partial solutions and Im wondering if they should be combined.
First, I didnt have CoreLocation added to my project, I need it right? To be constantly updating my user location and be able to calculate the distances to each point? Or does Mapkit somehow include a user location data that I can use?
Partial Solution A uses this code:
`-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if(!newLocation) return;
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude)){
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
CLLocationDistance distance = ([loc2 distanceFromLocation:loc1]) * 0.000621371192;
//distance = distance;
NSLog(#"Total Distance %f in miles",distance);
}
}
I understand this method calculates the distance between 2 points. I would somehow need to cycle thru my annotations and create the distance. It seems this would be the more useful one since it constantly recalculates the distances based on the current userLocation. Although, I do wonder about the effectiveness of that. Once you know how far away something is, you rarely wish to be constantly reminded as to how far away it is.
Partial Solution B uses this code:
`- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
CLLocation *pinLocation = [[[CLLocation alloc] initWithLatitude:[(MyAnnotation*)[view annotation] coordinate].latitude longitude:[(MyAnnotation*)[view annotation] coordinate].longitude]];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
NSLog(#"Distance to pin %4.0f", distance);
}
`
In this case, whenever the pin is tapped, the distance is calculated. But Im unclear as to the code for MyAnnotation [view annotation], Im guessing the original poster had his locations based off of a MyAnnotation Class so I changed it to MyLocation and all but 1 error went away. I get an Expected Identifier error at the pinLocation line at the last square bracket for some reason.
I feel the solution is in the tip of my tongue. Just need that little extra push :)
Thanks guys
Just move the code inside the calloutAccessoryControlTapped method right after the line, wherever you have it, that creates the MKAnnotation. Give MKAnnotation subclass a float distance property and set it as the subtitle.
I am developing an iPhone app. here in my App I have included Google maps.I am showing the highlighted path in map.
Now my Question is how to calculate distance between two point as per path as Apple"s native map App is showing routes with distance.
Thanks in Advance.
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.latitude];
CLLocationDistance distance = ([loc2 distanceFromLocation:loc1]) * 0.000621371192;
This may help you.
When running in the simulator, Core Location assigns a fixed set of coordinate values to this property. You must run your application on an iOS-based device to get real location values.
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.latitude];
I am using CLLocation to work out the distance from the current user location, and an annotation. However I just wanted to know if this would be correct. I am currently using iPhone Simulator for this and according to the MKMapView the iPhone Simulator is situated here:
Lat: 0 Long: -1067024384
The annotation's position is:
workingCoordinate.latitude = 40.763856;
workingCoordinate.longitude = -73.973034;
However if you take a look in google maps you will find out how close these distances are, yet so far apart according to CLLocation. I am using the following code to determine the distance between them both.
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc distanceFromLocation:loc2];
int distance = dist
NSLog(#"%i", distance);
The distance being NSLogged is 12769908. I believe that this is incorrect, and therefore there must be a problem with my code.
If there is please can you point it out!
You have two bad habits.
You should not depend on simulator in situations need hardware censor status. Especially when you want correct test.
You're handling types incorrectly. So you can't check values correctly. How is the longitude -1067024384? Longitude value is degrees. This means it's valid range is limited -90.0 ~ +90.0 by definition of longitude.
Your longitude value is out of range. This means one of these. You printed the value wrongly or the real value was wrong. Simulator can print wrong value. Or you printed the value with wrong method. You have to try:
Test on real device which has real hardware censors.
If bad result continues after that,
Review ALL of your application code.
Especially for printing, handling values. Check you're using correct types and castings in > each situations. Because you may did buggy operation in somewhere habitually.
And also, I recommend checking all of intermediate values like this.
CLLocationCoordinate2D annocoord = annotation.coordinate;
CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate;
NSLog(#"ANNO = %f, %f", annocoord.latitude, annocoord.longitude);
NSLog(#"USER = %f, %f", usercoord.latitude, usercoord.longitude);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
NSLog(#"LOC = %f, %f", loc.coordinate.latitude, loc.coordinate.longitude);
NSLog(#"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);
CLLocationDistance dist = [loc distanceFromLocation:loc2];
NSLog(#"DIST: %f", dist); // Wrong formatting may show wrong value!
Try #"%f" and don't cast it that way.
In CLLocation.h
typedef double CLLocationDistance;
I want to determine if a user has scrolled more than a certain percentage of the map then disable centering of the map from the user location (similar to how the Maps app works).
I'm not sure which methods to make use of.
I think it would be straightforward to create a rectangle and see if the rectangle contains the current center point, however I have to target IOS 3, so I can't make use of many of the newer Mapkit apis.
I've tried futzing with CLLocation, and using distanceFrom, between the current mapcenter, and the users location, but I'm trying to figure out if that distance is a certain percentage.
I personally find it more helpful when someone can post a snippet of code versus general prose about how one might go about this. Here's what I came up with- roughly hacked out to simply better answer this question:
In a header file I have:
#define SCROLL_UPDATE_DISTANCE 80.00
and in my view (that is both a delegate for CLLocationManagerDelegate, MKMapViewDelegate):
// this method is called when the map region changes as a delegate of MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(#"regionDidChangeAnimated");
MKCoordinateRegion mapRegion;
// set the center of the map region to the now updated map view center
mapRegion.center = mapView.centerCoordinate;
mapRegion.span.latitudeDelta = 0.3; // you likely don't need these... just kinda hacked this out
mapRegion.span.longitudeDelta = 0.3;
// get the lat & lng of the map region
double lat = mapRegion.center.latitude;
double lng = mapRegion.center.longitude;
// note: I have a variable I have saved called lastLocationCoordinate. It is of type
// CLLocationCoordinate2D and I initially set it in the didUpdateUserLocation
// delegate method. I also update it again when this function is called
// so I always have the last mapRegion center point to compare the present one with
CLLocation *before = [[CLLocation alloc] initWithLatitude:lastLocationCoordinate.latitude longitude:lastLocationCoordinate.longitude];
CLLocation *now = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
CLLocationDistance distance = ([before distanceFromLocation:now]) * 0.000621371192;
[before release];
[now release];
NSLog(#"Scrolled distance: %#", [NSString stringWithFormat:#"%.02f", distance]);
if( distance > SCROLL_UPDATE_DISTANCE )
{
// do something awesome
}
// resave the last location center for the next map move event
lastLocationCoordinate.latitude = mapRegion.center.latitude;
lastLocationCoordinate.longitude = mapRegion.center.longitude;
}
Hope that sends you in the right direction.
distanceFromLocation is iOS 3.2 and later.
initWithLatitude is iOS 2.0 and later.
MKCoordinateRegion is iOS 3.0 and later.
MKMapView centerCoordinate is iOS 3.0 and later.
Also- please feel free to jump in and set me straight where I've erred. I'm figuring all of this out myself- but this is working fairly well for me so far.
Hope this helps someone.
First lesson: Don't ask questions late night on SO.
Second lesson: you can achieve this simply by construction a CGPoint from the user's current location, and a CGPoint from the MapView center.
With two points, just calculate the distance, and see if it's past a certain threshold.
You can also construct a CGRect around the map center, and check CGRectContainsPoint if that's easier.
- (BOOL) isUserPointInsideMapCenterRegion
{
CLLocation * ul = _mapView.userLocation.location;
CGPoint userPoint = [_mapView convertCoordinate: ul.coordinate toPointToView: _mapView];
CGPoint mapPoint = [_mapView convertCoordinate: _mapView.centerCoordinate toPointToView: _mapView];
if (fabs(userPoint.x - mapPoint.x) > MAP_CENTER_RECTANGLE_SIZE || fabs(userPoint.y - mapPoint.y) > MAP_CENTER_RECTANGLE_SIZE)
{
return NO;
}
return YES;
}
I realise this question is a bit old now, but I feel that the answer described in this other question is more robust because the delegate method could be fired for any reason. Using a UIPanGestureRecognizer to detect the scroll means that the user manually scrolled the map, and it can check if the map has scrolled X pixels, instead of relying on meters, which means the user has scrolled more or less depending on the zoom level.
https://stackoverflow.com/a/11675587/159758
I'm attempting to talk to a web service for locations within the zoomed-in or zoomed-out area on an embedded MKMapView. In the regionDidChangeAnimated method of my view controller (this is the method that I use to trap any user gesture on the map), I call the following:
NSLog( #"latitude delta = %f", mapView.region.span.latitudeDelta );
NSLog( #"longitude delta = %f", mapView.region.span.longitudeDelta );
And the log entry says:
latitude delta = 0.000435
longitude delta = 0.001930
However, if I requery the lat/long dela manually after the regionDidChangeAnimated has fired (i.e. the user gesture is completed), I get the following:
latitude delta = 0.008415
longitude delta = 0.011932
Why is there a difference here? It doesn't matter whether this is a zoom in or zoom out gesture. There is always a difference. What's up with this??
I submitted this bug to Apple, and was informed that this is a known issue. No workaround was suggested. :-(