In my mapView application i am trying to make the annotation, Without adding annotation i am getting the mapView but when i try to add annotation, Only the annotation mark is visible the map becomes invisible.
Before adding Annotation:
Code:
[super viewDidLoad];
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, elf.view.frame.size.height)];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
[self.view addSubview:mapView];
Image:
After Adding Annotation:
Code:
[super viewDidLoad];
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
[self.view addSubview:mapView];
MKUserLocation *userLocation = mapView.userLocation;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 20, 20);
[mapView setRegion:region];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = userLocation.location.coordinate;
annotation.title = #"Here you r";
annotation.subtitle = #"Pondy";
[mapView addAnnotation:annotation];
Image:
I need the annotation appear in the map. Thanks in advance.
Try changing the value of MKCoordinateRegionMakeWithDistance.
it add anotation of your current location...You put span 20, 20. That is very close so only it takes some time to load
mapView1.showsUserLocation = YES;
MKUserLocation *userLocation = mapView1.userLocation;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 200, 200);
[mapView1 setRegion:region];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = userLocation.location.coordinate;
annotation.title = #"Title";
annotation.subtitle = #"Sub Title";
[mapView1 addAnnotation:annotation];
I hope it will be work for you
Blue screen usually means your current location is in the sea as discussed above.
Please check the current location which you are getting is correct or not.
Usually for showing current location in MKMap we write map.showuserlocation=YES and it shows the bluew dot in the map view if you have to change the annotation pin in your map of the currentlocation you can go in view for annotation methiod and check for the userlocation class and hence change the pin of your userlocation annotation.
I think that at the time you add your annotation, the user location is still unknown:
MKUserLocation *userLocation = mapView.userLocation;
// At that time maybe the userLocation hasn't been retrieved through the GPS yet
...
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = userLocation.location.coordinate;
annotation.title = #"Here you r";
annotation.subtitle = #"Pondy";
[mapView addAnnotation:annotation];
In that case, the annotation position will be 0, 0 and as your map region is also based on the user location from the mapView, you'll end in the middle of the sea at position 0,0.
In order to monitor changes of the user location of the mapView, and then readjust your pin and / or the map region only when you're sure that the userLocation given by the mapView is correct, just implement the MKMapViewDelegate protocol for this selector :
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
then, in the body of your implementation, do what you did previously, but now, you can check if the user location is correct or not, and only adjust your map region and annotation once you're sure that the user location is OK.
The reason is location of your simulator is set to NONE.
Follow these steps, you will get your answer...
`
1) Open your simulator.
2) go to Debug menu
3) Select Location.
3) Select Custom Location. (OR Select location of Apple to skip step
No 4.)
4) Enter your Latitude and Longitude.
5) Delete your app from simulator.
6) Run your project again.
`
Enjoy....
Related
I'm pretty new on programmin to develop iphone applications and i would like to know why the MKPolyline that I create with 2 MKMapPoints doesn't appears in the MKMapView that I insert on my view.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
map = [[MKMapView alloc] initWithFrame:self.view.bounds];
MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*4);
CLLocationCoordinate2D punto1;
punto1.latitude =39.468502;
punto1.longitude =-0.398469;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = punto1;
annotationPoint.title = #"Point 1";
MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc]init];
annotationPoint2.coordinate = CLLocationCoordinate2DMake(39.472312,-0.386453);
annotationPoint2.title = #"Point 2";
[map addAnnotation:annotationPoint];
[map addAnnotation:annotationPoint2];
pointsArray[0]= MKMapPointForCoordinate(punto1);
pointsArray[1]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.467011,-0.390015));
pointsArray[2]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.469926,-0.392118));
pointsArray[3]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.472312,-0.386453));
routeLine = [MKPolyline polylineWithPoints:pointsArray count:4];
free(pointsArray);
[map addOverlay:routeLine];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(39.467011,-0.392515), 1100, 1100);
[map setRegion:region];
[self.view insertSubview:map atIndex:0];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
{
MKOverlayView* overlayView = nil;
MKPolylineView * routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
routeLineView.fillColor = [UIColor blueColor];
routeLineView.strokeColor = [UIColor orangeColor];
routeLineView.lineWidth = 3;
overlayView = routeLineView;
return overlayView;
}
The annotations are OK and they show correctly on the map.
Hope someone can help, thanks!!!
The MKPolyline doesn't show because the map's delegate is not set.
The viewForOverlay delegate method won't get called if the map's delegate property is not set. Because the delegate method is never called, the MKPolylineView never gets created, etc...
After creating the MKMapView, set its delegate:
map = [[MKMapView alloc] initWithFrame:self.view.bounds];
map.delegate = self; // <-- add this
I'd like to mention a few other unrelated points:
Since you are putting MKMapPoint values in the pointsArray, the malloc should use sizeof(MKMapPoint) instead of sizeof(CLLocationCoordinate2D). It happens to work with the wrong code because both structs happen to be the same size. But you should still use the right code.
MKPolyline also has a polylineWithCoordinates:count: method so you can pass CLLocationCoordinate2D instead of MKMapPoint structs. This can be easier to read, understand, and avoids having to convert from coordinates to mappoints.
The MKPolylineView uses strokeColor only so setting the fillColor for it does nothing.
In the viewForOverlay delegate method, it's much better practice to use the overlay parameter that is passed into the method instead of the externally declared routeLine. This will be extremely important if you want to add more than one overlay. You would also first check what kind of class overlay is and then create the appropriate view for it.
So I have set my app to search for the user's location upon pressing on a button.
-(IBAction)getLocation {
mapview.showsUserLocation = YES;}
Then I have set a Map Annotation as well to a certain location upon loading of the map view using this
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 123;
region.center.longitude = 123;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Place to go to";
ann.subtitle = #"subtitle";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
Is it possible to be able to put a "Directions to Here" button to my annotation using MapKit? Any help would be awesome! Thanks!
To add the callout accessory, you have to follow the instruction from Anna Karenina.
To draw the route itself, you have to first obtain the route, there are plenty of excellent APIs that will give you the route between two given points, even allowing you to set params like if the kind of routes you want to include. Google Directions API is pretty impressive on that regard. You should check it.
Then you have to draw the route itself with MKPolyline. You can check a toy app i put together a few months ago to show how to do this.
I have a lot of pin annotations on the MKMapView in my app, the iPhone gets very slow and unresponsive when a lot of them are in view on the map. I would like the user to be able to zoom, but not out past a certain level, such as 2km squared or something.
Here's what I've got:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[BicycleLDNService sharedService] requestLocationForClient:self];
CLLocationCoordinate2D zoomLocation;
CLLocation *deviceLocation = [[BicycleLDNService sharedService] deviceLocation];
zoomLocation.latitude = deviceLocation.coordinate.latitude;
zoomLocation.longitude = deviceLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*kMetresPerKilometre, 0.5*kMetresPerKilometre);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.zoomEnabled = YES;
}
Is there some sort of property or delegate method I can employ? Couldn't find anything useful on google or here!
Thanks in advance!
The span defines how much of the map at the given point should be visible and is also how you set the zoom level.
You can access this by using
region.span.latitute=0.5;
region.span.longitude=0.6;
Check the zoom level of the map and then set the zoomEnabled Property NO.
mapView.zoomEnabled=NO;
I've got a mapView which zooms to the current location using viewDidLoad :
#define METERS_PER_MILE 1609.344
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation=TRUE;
// zoom to a specific area
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = -28.994167;
zoomLocation.longitude = 134.866944;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
// make sure the Google water mark is always visible
mapView.autoresizingMask =
(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[mapView setRegion:adjustedRegion animated:YES];
mapView.delegate=self;
searchBar.delegate = self;
}
This works fine. I've added a search bar and a function to jump to a specific address location. This works fine, too. I now want to add a button to jump back to the current location. Can you give me a hand, please?
Cheers
You need to set the center of your map to the current location on tap of that button. Say, like this:
- (IBAction)showCurrentLocation {
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
you can also try:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
You can link this IBAction to your UIButton,
it's going to move the map on the current location and zoom on it.
#IBOutlet weak var mapView: MKMapView!
#IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
if self.mapView != nil {
self.mapView.setRegion(MKCoordinateRegionMake(
self.mapView.userLocation.coordinate,
MKCoordinateSpanMake(0.1, 0.1)
), animated: true)
}
}
MKCoordinateSpan defines the area spanned by a map region, smaller these values are, closer you zoom on the map.
- (void)showCurrentLocation{
MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
[self.mapView setVisibleMapRect:zoomRect animated:YES];
}
FOR SWIFT
Add this line in button action yourMKMapView.setUserTrackingMode(.follow, animated: true)
make sure you add yourMKMapView.showsUserLocation = true in viewDidLoad()
I have one application that shows maps. I need to show current position of user and manage to do that with
[mapView setShowsLocation:YES];
But when I zoom In or zoom Out map it needs a lot of time to show me that blue pin again.
Is it normal or I need to put something else to keep that blue pin on screen all time???
Thanks.
[self setCurrentLocation:self._mapView.userLocation.location.coordinate withZoom:1.0];
self._mapView.showsUserLocation = YES;
- (void)setCurrentLocation:(CLLocationCoordinate2D)coord withZoom:(float)zoomLevel {
MKCoordinateRegion region = self._mapView.region;
region.span.latitudeDelta = self.defaultSpanLevel.latitudeDelta*zoomLevel;
region.span.longitudeDelta = self.defaultSpanLevel.longitudeDelta*zoomLevel;
region.center = coord;
[self._mapView setRegion:region animated:YES];
}
You can use this in view will appear Method
Then You can use mapview delegate methods....
region did change animated:YES { and set map's region here.... take
current user location as region centre.. }
This will solve your problem
}