Reputation: 556
I'm having a few issues with MKMapView! I'm new to the mapping! What i'm trying to do:
Mapview loads up and drops a placemarker on user's current location, and on the address of the selected customer. This code drops a placemarker on the address of the selected customer fine, but i can't seem to get it to drop one on the user's current location!
Any ideas why?
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:postcodeForMap
completionHandler:^(NSArray* placemarks, NSError* error){
NSLog(@"Co-ordinate geocode is %@", [placemarks objectAtIndex:0]);
for (CLPlacemark* aPlacemark in placemarks)
{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPlacemark *mPlacemark = [[MKPlacemark alloc] initWithCoordinate:mapView.userLocation.location.coordinate addressDictionary:nil];
MKCoordinateRegion region = mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[mapView setRegion:region animated:YES];
[mapView addAnnotation:placemark];
[mapView addAnnotation:mPlacemark];
}
}
];
Upvotes: 0
Views: 1953
Reputation: 2136
For Your consideration. It is easy to drop any image or url or pin to user current location.
For that you have to implement.
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if (annotation == mapView.userLocation)
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
return annView;
}
}
Upvotes: 1
Reputation: 12717
You can get user location using the following code
-(void)viewDidLoad{
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;//THIS IS IMPORTANT
[locationManager startUpdatingLocation];
}
When you tell the location manager to get the locations if Allowed
the locationmanager
calls its delegate method see below
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//newLocation is your current Location
self.userLocation=newLocation;//Once you got the location generate the places..
[self generatePlaces];
[manager stopUpdatingLocation];
}
-(void)generatePlaces{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:postcodeForMap
completionHandler:^(NSArray* placemarks, NSError* error){
NSLog(@"Co-ordinate geocode is %@", [placemarks objectAtIndex:0]);
for (CLPlacemark* aPlacemark in placemarks)
{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
//EDITED
MKPlacemark *mPlacemark = [[MKPlacemark alloc] initWithCoordinate:self.userLocation.coordinate addressDictionary:nil];
MKCoordinateRegion region = mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[mapView setRegion:region animated:YES];
[mapView addAnnotation:placemark];
[mapView addAnnotation:mPlacemark];
}
}
];
}
Upvotes: 1