I followed this question: iOS - MKMapView place annotation by using address instead of lat / long - to create a map annotation for a postal code as opposed to the long/lat values directly.
This works fine, however I would like to set the title and subtitle of the anno
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
placemark.title = self.business.businessName;
placemark.subtitle = self.business.phoneNumber;
This is not working as the title and subtitle are readonly. How can I change the above so that I am able to set the title and subtitle?
Use MKPointAnnotation instead.
Sample Code :
CLPlacemark *topresult = [placemarks objectAtIndex:0];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = topresult.location.coordinate;
annotation.title = self.business.businessName;
annotation.subtitle = self.business.phoneNumber;
[self.mapView addAnnotation:annotation];
You can try below code. It may help you.
//set the title if we got any placemarks...
if (placemark.count > 0)
{
CLPlacemark *topResult = [placemark objectAtIndex:0];
annTitle = [NSString stringWithFormat:#"%# %# %# %#", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
}
//now create the annotation...
MapAnnotation *toAdd = [[MapAnnotation alloc]init];
toAdd.coordinate = touchMapCoordinate;
toAdd.title = #"Address";
toAdd.subtitle = #"Sub Address";
[self.map addAnnotation:toAdd];
Related
I am trying to annotate some locations in map using MapBox. and its done.
But the problem is some other annotations are seen in map without any reference, i.e we cant click on it and when i zoom in/zoom out it disappears.
Am only try to create two annotations including user location.
How this happen?
is it because any reusing of annotation?
Some code snippets used:
1) creating annotation in loop
MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = coordinate;
point.title = location;
NSString *altString =[NSString stringWithFormat:#"%#",mIsNSDictionary(response)?[response objectForKey:kKeyAlt]:response];
NSString *str = [NSString stringWithFormat:kKeyLocationLatLonNAltInBaidu,coordinate.latitude,coordinate.longitude,[altString floatValue]];
point.subtitle = str;
[self.arrayAnnotations addObject:point];
[self.mapView addAnnotation:point];
Note: self.arrayAnnotations contains only 2 points
2) Delegate method for annotation
-(MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
if([annotation isKindOfClass:[MGLPointAnnotation class]] && [self.arrayAnnotations containsObject:annotation]) {
NSString *reuseIdentifier = [self makeIdentifierString:annotation];
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:reuseIdentifier];
if (!annotationImage) {
annotationImage = [MGLAnnotationImage annotationImageWithImage:[UIImage imageNamed:kImageNameMarker] reuseIdentifier:reuseIdentifier];
}
return annotationImage;
}
return nil;
}
I'm using the below code to display addresses from an array (responseObject) as annotations on my mapview. It works, and the pin is dropped successfully from my location string, however it only shows a pin for the most recent address added to the array. How can I change my code so that it shows pins on the map for all addresses in my array instead of just the most recent one? Apologies if this is a newb question. Thanks!
viewcontroller.m
NSMutableDictionary *viewParams = [NSMutableDictionary new];
[viewParams setValue:#"u000" forKey:#"view_name"];
[DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.addressData = [responseObject mutableCopy];
NSString *location = self.addressData[0][#"address"];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
// region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = self.addressData[0][#"users_name"];
point.subtitle = self.addressData[0][#"userbio"];
[self.mapView addAnnotation:point];
You are accessing only one object ?
NSString *location = self.addressData[0][#"address"];
Edited
I think you should handle your data, separated with your view. i.e. implement geocoder related code in the mapView:viewForAnnotation: method in your map view delegate. Then you should be able to create the annotations one by one and use [self.mapView addAnnotations] for all of them
For your code, which I believe is inspired by this answer, you should be able to iterate through all location addresses by something like
for (NSMutableDictionary *loc in self.addressData) {
NSString *loc = location[#"address"];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
......
}
Forgive me if the syntax is wrong for Objective C.
I got a very weird problem when I tried to call iOS map for navigation from current location to a location in my app. The routes were correctly calculated, but the destination location doesn't have a pin in the map, the current location has the pin though. My code is like the following. I think I'm missing something really basic but can't figure out.... Any thoughts?
(void) routeInMap
{
CLLocationCoordinate2D location;
location.latitude = [[thisAttraction objectForKey:#"lat"] floatValue];
location.longitude = [[thisAttraction objectForKey:#"lng"] floatValue];
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:[thisAttraction objectForKey:#"placeAddress"]
completionHandler:^(NSArray *placemarks, NSError *error) {
// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:location
addressDictionary:geocodedPlacemark.addressDictionary];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
// 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];
}];
}
}
I've created a route from A to B with MKMapItem, but I can't change the title of the annotation on the map. My code:
CLLocation *locationRestaurante = [[CLLocation alloc] initWithLatitude:[[[listaRestaurante objectAtIndex:0] objectForKey:#"latitude"] floatValue] longitude:[[[listaRestaurante objectAtIndex:0] objectForKey:#"longitude"] floatValue]];
MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:locationRestaurante.coordinate addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
NSDictionary *options = #{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
[mapItem openInMapsWithLaunchOptions:options];
it opens everything just fine... But there's only one problem: The I can't change this annotation name:
mapItem.name = #"Place name";
MKMapItem Class Reference
I'm having issues setting the title and subtitle of my placemark.
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
placemark.title = #"Some Title";
placemark.subtitle = #"Some subtitle";
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
}
];
placemark.title = #"Some Title"; and placemark.subtitle = #"Some subtitle";
give me an error of:
Assigning to property with 'readonly' attribute not allowed
Why can I not set the Title and Subtitle here?
Thought I'd wake this thread up and give you a solution that I came up with.
As far as I'm aware, MKPlacemark's title/subtitle are readonly properties due to inherent assignment. However, with the solution I found, you can simply pass your MKPlacemark into an MKPointAnnotation as follows:
CLPlacemark *topResult = [placemarks objectAtIndex:0];
// Create an MLPlacemark
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
// Create an editable PointAnnotation, using placemark's coordinates, and set your own title/subtitle
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = #"Sample Location";
point.subtitle = #"Sample Subtitle";
// Set your region using placemark (not point)
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
// Add point (not placemark) to the mapView
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:point];
// Select the PointAnnotation programatically
[self.mapView selectAnnotation:point animated:NO];
Please note that the final [self.mapView selectAnnotation:point animated:NO]; is a workaround to allow automatic popping-up of the placemark. However, the animated:BOOL portion only seems to work with NO in iOS5 - You might want to implement a workaround if you experience issues manually popping up the point-annotation, which can be found here:
MKAnnotation not getting selected in iOS5
I'm sure you've already found your own solution by now, but I hope this is, in some way, informative.