i added MapView and and required condition for showing current location , and with button functionality added just this method from apple documentation.
- (IBAction)refreshMapMethod:(id)sender
{
double latitude = 39.281516;
double longitude =-76.580806;
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:#"Name of your location"];
[mapItem openInMapsWithLaunchOptions:nil];
/////
////////
CLLocation* fromLocation = mapItem.placemark.location;
// Create a region centered on the starting point with a 10km span
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(fromLocation.coordinate, 10000, 10000);
// Open the item in Maps, specifying the map region to display.
[MKMapItem openMapsWithItems:[NSArray arrayWithObject:mapItem]
launchOptions:[NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithMKCoordinate:region.center], MKLaunchOptionsMapCenterKey,
[NSValue valueWithMKCoordinateSpan:region.span], MKLaunchOptionsMapSpanKey, nil]];
}
after pressing button i am getting new view with all the functionality like add annotation on map view with long press, tracing path and many more .how this is happening with small function
any idea pleas help .
Both openInMapsWithLaunchOptions: and openMapsWithItems:launchOptions: will open the Apple Maps application (and put your application into the background). Probably this isn't what you intended.
Related
I am trying to find how to provide directions to a certain location using the in app MapView. I want my app to pull the users current location(which I have accomplished), then I want the MapView to use that information to provide turn by turn directions to a certain destination. Is this even possible with iOS SDK 6.0 and xcode 4.5?
U can Use Google Directional Map api .simply Implement
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([_iLat doubleValue],[_iLong doubleValue]);
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:placeMark];
if([destination respondsToSelector:#selector(openInMapsWithLaunchOptions:)])
{
//using iOS6 native maps app
[destination openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];
}
I wondered if it is possible to create navigation route within MKMapView on iOS 6 instead of opening the built-in Maps application?
I googled it but didn't find any answer.
This is the code I'm using now (that is opening the built-in Maps app):
-(IBAction) navigation
{
if (SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
NSString *destinationString = #"Afek, Israel";
NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",wedMapView.userLocation.coordinate.latitude,wedMapView.userLocation.coordinate.longitude, destinationString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
else
{
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
double latitude = 32.83431728836292;
double longitude = 35.128666162490845;
MKPlacemark *placemark = [[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil] autorelease];
//[mapItem setName:#"Name of your location"];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:#"שמורתה, קיבוץ אפק"];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = #{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:#[currentLocationMapItem, mapItem] launchOptions:launchOptions];
};
}
}
And this is the MKMapView I Have that I want to make turn by turn route when I press the blue map button (above the Waze button):
Thanks for the help.
iOS 6 does not provide the app with directions, it relies on the built-in Maps app to do that. So if you want to show directions with in your app you'll need to find a service for calculating the route. You can't use Google because they only allow you to use their data on their maps, not on Apple's. You could look into CloudMade and OpenStreetMap, but you'd have to give up using iOS 6's maps for that. They come with their open UI components.
I am implementing a mapView whereby an annotation will be placed when the user search for an address. But somehow, the annotation sometime doesn't move and update to the new coordinate. It's only upon zooming the map, then will it update to the new location. The subtitle did get updated though.
- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
SVGeocoder *geocodeRequest = [[SVGeocoder alloc] initWithAddress:searchBar.text inRegion:#"sg"];
[geocodeRequest setDelegate:self];
[geocodeRequest startAsynchronous];
}
- (void)geocoder:(SVGeocoder *)geocoder didFindPlacemark:(SVPlacemark *)placemark {
if (annotation) {
[annotation moveAnnotation:placemark.coordinate];
annotation.subtitle = [NSString
stringWithFormat:#"%#", placemark.formattedAddress];
}
else {
annotation = [[MyAnnotation alloc]
initWithCoordinate:placemark.coordinate
title:#"Tap arrow to use address"
subtitle:[NSString
stringWithFormat:#"%#", placemark.formattedAddress]];
[mapView addAnnotation:annotation];
}
MKCoordinateSpan span;
span.latitudeDelta = .001;
span.longitudeDelta = .001;
MKCoordinateRegion region;
region.center = placemark.coordinate;
region.span = span;
[mapView setRegion:region animated:TRUE];
[searchBar resignFirstResponder];
}
I don't think MKMapView gets notified of changes to an annotation's location. The documentation of MKAnnotation's setCoordinate: says: "Annotations that support dragging should implement this method to update the position of the annotation." so it seems that's the only purpose of this method is for supporting dragging of pins.
Try removing the annotation from the map view before changing its coordinates and then add it back to the map view.
Nothing in your code (that you've shown) tells mapView that the annotation's location has changed. The annotation itself probably can't do it in -moveAnnotation because annotations generally don't know what map or maps they've been added to (nor should they).
The right way to move an annotation is to to remove it from the MKMapView that's using it, update its location, and then add it back to the map. You can't just change the annotation's location after it has been added to the map because the map may very well cache the location or sort the annotation according to its location, and there's no method in MKMapView to tell the map that the location has changed.
I'd change your conditional to something like this:
if (annotation == nil) {
annotation = [[MyAnnotation alloc] init];
annotation.title = #"Tap arrow to use address";
}
[mapView removeAnnotation:annotation];
[annotation moveAnnotation:placemark.coordinate];
annotation.subtitle = placemark.formattedAddress;
[mapView addAnnotation:annotation];
This assumes that it's safe to call -init in place of -initWithCoordinate:title:subtitle:; if not, you'll want to change that.
I have this code to set the map at the user's location:
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location = mapView.userLocation.location.coordinate;
MKCoordinateRegion region;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
Now when I do mapView.showsUserLocation=YES it shows my location at the right place (on my device, and the apple hq in the simulator) however, both times the code above puts me in the sea somewhere near africa!
Any ideas?
The latitude/longitude pair 0/0 is on the equator due south of Greenwich, England. That spot is in the Gulf of Guinea, off the West coast of Africa. If that's where your pin is getting placed, then your mapView.userLocation isn't getting set.
Probably your code here is running before MKMapKit has had a chance to get its location. What you really want to do is have your viewController adopt the MKMapKitDelegate protocol, set the .delegate property of your map view to self, and then implement the method:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
That method will get called when the map kit locates the user, and you can use the userLocation value you get passed there to set the region on the map. Until that gets called the first time, though, the MapView is still working on locating itself, and your asking about its location is premature.
Also, your regionThatFits there at the end is a no-op. That method doesn't resize the region in place, it actually returns a resized region, for you to use however you will. So you want to set your map view's region to what that method returns. Like this:
[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];
What I have done was add the following entry on info.plist:
NSLocationAlwaysUsageDescription
Got this info into this (very good) tutorial:
https://www.youtube.com/watch?v=Z272SMC9zuQ
You can choose this method..
- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated{
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:_mapView.region.center.latitude longitude:_mapView.region.center.longitude];
CLLocationAccuracy accuracy = _mapView.userLocation.location.horizontalAccuracy;
if (accuracy) {
current_pickup_location = loc;
[_currentStreetSpinner startAnimating];
[ceo reverseGeocodeLocation:loc
completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSString *locatedAt = [[placemark.addressDictionary valueForKey:#"FormattedAddressLines"] componentsJoinedByString:#", "];
// NSLog(#"location %#",placemark.location);
NSLog(#"I am currently at %#",locatedAt);
_currentStreetLabel.text = locatedAt;
static_iVar = _currentStreetLabel.text;
[_currentStreetSpinner stopAnimating];
[self listDoctorsWithSpeciality_id:currentSpeciality_id
withProfessionalityId:currentProfessionality_id];
}];
}
}
I am answering this question because the answer picked did not really help me and I was having the exact same issue. didUpdateUserLocation is not very practical because it makes the app update every single time the location changes. The other issue with the above solution is that when the map loads it's still too early to ask for a location. So how do you ask for user location before the the map loads so that the zooming doesn't take you to Africa?
You need to request the location by using the location manager on the viewDidLoad. Then after that you can set the region.
SWIFT 3
//top of your class
let locManger = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
//request user location
locManager.startUpdatingLocation()
//this is the code that sets the region based on the location
let span = MKCoordinateSpanMake(0.5, 0.5)
let location = CLLocationCoordinate2DMake((locManager.location?.coordinate.latitude)!, (locManager.location?.coordinate.longitude)!)
let region = MKCoordinateRegionMake(location, span)
yourMap.setRegion(region, animated: false)
//stop location updating
locManager.stopUpdatingLocation()
}
I am just looking at mapKit and decided to make a quick button to display my current location, however when I press the button my latitude/longitude always display as [0.000000] [0.000000].
The mapView is loaded as I can see the map on the simulator before I press the button. Previously I have done this by using coreLocation.framework and using CLLocationManager and asking for the device location that way. I am just curious why this way is not working correctly, would I be better doing this via CLLocationManager?
-(IBAction)findMePressed {
MKUserLocation *myLocation = [myMapView userLocation];
CLLocationCoordinate2D coord = [[myLocation location] coordinate];
[myMapView setCenterCoordinate:coord animated:YES];
NSLog(#"findMePressed ...[%f][%f]", coord.latitude, coord.longitude);
}
EDIT: Added ...
-(IBAction)findMePressed {
MKUserLocation *myLocation = [myMapView userLocation];
CLLocationCoordinate2D coord = [[myLocation location] coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 350, 350);
[myMapView setRegion:region animated:YES];
}
-(void)viewDidLoad {
[super viewDidLoad];
[myMapView setShowsUserLocation:YES];
}
Gary.
Either the userLocation is not visible on the map (see the userLocationVisible property) or there is some problem setting up the myMapView property and it's nil (i.e. not connected in interface builder)
[...] as I can see the map on the simulator [...]
Test it on the device. By default, on the simulator, the coordinates you get back are Apple's headquarters. Cf. doc.
See this other SO question for workarounds and useful utilities : Testing CoreLocation on iPhone Simulator