I have a UITableView that opens a view with a map, i called Mapa class. I am having problems passing any kind of text information from this table view to the map. I need to send a string text to be the title of my map, and the coordinates of a CLLocation.
Here is part of the code
MyTableView.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
instMapa = [[Mapa alloc] initWithNibName:#"Mapa" bundle:nil];
instMapa.mytext = #"pass text to be the title";
[self.navigationController pushViewController:instMapa animated:YES];
}
Mapa.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#class MapViewAnnotation;
#interface Mapa : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
NSString *stringTitle;
NSString *mytext;
MapViewAnnotation *newAnnotation;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (nonatomic, retain) NSString *stringTitle;
#property (nonatomic, retain) NSString *mytext;
#property (nonatomic, retain) MapViewAnnotation *newAnnotation;
#end
Mapa.m
#import "Mapa.h"
#import "MapViewAnnotation.h"
#implementation Mapa
#synthesize mapView;
#synthesize stringTitle;
#synthesize mytext;
#synthesize newAnnotation;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationCoordinate2D location;
location.latitude = (double) 51.501468;
location.longitude = (double) -0.141596;
// Add the annotation to our map view
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:mytext andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
}
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
[mv setRegion:region animated:YES];
[mv selectAnnotation:mp animated:YES];
}
// Received memory warning
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
// If the view unloads, release the map view
- (void)viewDidUnload {
}
- (void)dealloc {
[newAnnotation release];
[mytext release];
[stringTitle release];
[mapView release];
[super dealloc];
}
#end
MapViewAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
#end
MapViewAnnotation.m
#implementation MapViewAnnotation
#synthesize title, coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
[super init];
title = ttl;
coordinate = c2d;
return self;
}
- (void)dealloc {
[title release];
[super dealloc];
}
#end
Thanks for the help!
From your question, I understand that; you need to pass the string value of the selected table view cell to your map view.
For that you need to write this code in your didSelectRowAtIndexPath,
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
instMapa.mytext = cell.textLabel.text;
Related
I am new in objective c.I have created a project which consist of a mapView
In ViewController.h in ny project,
#import <UIKit/UIKit.h>
#import "MapKit/MapKit.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
In ViewController.m file i have viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
// Do any additional setup after loading the view, typically from a nib.
}
I want to give coordinates of more then one location in my code, and I want to show loc.png icon on map corresponding to those coordinates. how can I accomplish this task? And how to zoom the map to maximum scale ?
Form this link you can download my sample project: https://drive.google.com/file/d/0B5pNDpbvZ8SnZGZnU1ZfbjZMRWs/view?usp=sharing
I had working on your question and this are my results, use this code, now Iam using a custom class that implements the MKAnnotation protocol
EDITED
Place.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Place : NSObject<MKAnnotation>
#property (nonatomic) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
#property (nonatomic, nullable) NSString *title;
#property (nonatomic, nullable) NSString *subtitle;
-(id)initWithCoordinates:(CLLocationCoordinate2D) coordinates andName:(NSString*)name;
#end
Place.m
#import "Place.h"
#implementation Place
#synthesize coordinate,title,subtitle;
-(id)initWithCoordinates:(CLLocationCoordinate2D) coordinates andName:(NSString*)name
{
self = [super init];
if(self)
{
[self setTitle:name];
[self setCoordinate:coordinates];
}
return self;
}
#end
Modified Code
#import "ViewController.h"
#import "Place.h"
#interface ViewController () <MKMapViewDelegate>
#property NSMutableArray * arrayOfLocations;
#end
#implementation ViewController
#synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
self.arrayOfLocations = [NSMutableArray arrayWithObjects:[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(40.416691, -3.700345) andName:#"MADRID"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(35.416691, -3.700345) andName:#"SOMEWARE IN THE MAP"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(35.416691, -40.700345) andName:#"SOMEWARE IN THE MAP1"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(20.416691, -50.700345) andName:#"SOMEWARE IN THE MAP2"], nil];
[self.mapView setDelegate:self];
[self.mapView addAnnotations:self.arrayOfLocations];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"testAnnotationView"];
if(annotationView == nil){
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"testAnnotationView"];
annotationView.image = [UIImage imageNamed:#"loc.png"];
annotationView.canShowCallout = true;
}
return annotationView;
}
#end
Hope this helps you,
This is how it looks
I would like to have a MKMapView showing annotations with disclosure-buttons which lead to a view controller like the Golden Gate Bridge annotation in this Apple sample app.
I load the coordinates from a plist and the annotations appear correctly with title/subtitle but the method
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
has no effect.
I guess that I somehow have to link the annotations with the pinannotations?
MapViewController.h:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"
#interface MapViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>
#property (strong, nonatomic) CLLocationManager *location;
#property (nonatomic, retain) NSArray *data;
#end
MapViewController.m:
#import "MapViewController.h"
#interface MapViewController ()
#property (nonatomic, weak) IBOutlet MKMapView *mapView;
#end
#implementation MapViewController
#synthesize data;
#synthesize location, minLatitude, maxLatitude, minLongitude, maxLongitude;
- (void)viewDidLoad
{
NSString *dataPath = [[NSBundle mainBundle] pathForResource:#"City" ofType:#"plist"];
self.data = [NSArray arrayWithContentsOfFile:dataPath];
for (int i = 0; i < data.count; i++) {
NSDictionary *dataItem = [data objectAtIndex:i];
//Create Annotation
Annotation *building = [[Annotation alloc] init];
building.title = [dataItem objectForKey:#"Title"];
building.subtitle = [dataItem objectForKey:#"Subtitle"];
MKCoordinateRegion buildingcoordinates = { {0.0, 0.0}, {0.0, 0.0} };
buildingcoordinates.center.latitude = [[dataItem objectForKey:#"Latitude"] floatValue];
buildingcoordinates.center.longitude = [[dataItem objectForKey:#"Longitude"] floatValue];
building.coordinate = buildingcoordinates.center;
[self.mapView addAnnotation:building];
}
[super viewDidLoad];
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *pinIdentifier = #"pinIndentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
if (pinView == nil)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
Annotation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface Annotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString *title;
#property(nonatomic, copy) NSString *subtitle;
#end
Annotation.m:
#import "Annotation.h"
#implementation Annotation
#synthesize coordinate, title, subtitle;
#end
Most likely the map view's delegate is not set which means the viewForAnnotation delegate method will not get called.
Since you've declared mapView as an IBOutlet, in the xib, make sure that the map view's delegate is connected to File's Owner.
Alternatively, at the top of the viewDidLoad method in MapViewController, set it programmatically:
mapView.delegate = self;
I have followed this tutorial:
http://www.shawngrimes.me/2011/04/custom-map-pins-for-mapkit/#comment-193
but I can't add a title and description
(see my code here http://pastebin.com/03mDLc9q)
You are trying to set name and description as properties of your annotation's view, you should be using title and subtitle on your annotation object - MyAnnotationClass, the annotation view will use title and subtitle of this object when the callout is rendered.
I changed your code to work here: http://pastebin.com/YRGYhQev
#interface MyAnnotationClass : NSObject<MKAnnotation>
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;
#end
MyAnnotationClass.m
#import "MyAnnotationClass.h"
#implementation MyAnnotationClass
-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate{
self=[super init];
if(self){
_coordinate = coordinate;
}
return self;
}
-(void) dealloc{
[_title release];
[_subtitle release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController<MKMapViewDelegate> {
IBOutlet MKMapView *_myMapView;
NSArray *_myAnnotations;
}
#property (nonatomic, retain) NSArray *myAnnotations;
#property (nonatomic, retain) IBOutlet MKMapView *myMapView;
#end
ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "PlaceMark.h"
#import "MyAnnotationClass.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myMapView = _myMapView;
#synthesize myAnnotations = _myAnnotations;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void) viewDidLoad{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//Initialize annotation
MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
commuterLotAnnotation.title = #"Hello title";
commuterLotAnnotation.subtitle = #"Correct";
MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
overflowLotAnnotation.title = #"Hello title";
overflowLotAnnotation.subtitle = #"Correct";
//Add them to array
self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];
//Release the annotations now that they've been added to the array
[commuterLotAnnotation release];
[overflowLotAnnotation release];
//add array of annotations to map
[_myMapView addAnnotations:_myAnnotations];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
static NSString *parkingAnnotationIdentifier=#"ParkingAnnotationIdentifier";
if([annotation isKindOfClass:[MyAnnotationClass class]]){
//Try to get an unused annotation, similar to uitableviewcells
MKAnnotationView *annotationView=[_myMapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
//If one isn't available, create a new one
if(!annotationView){
annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
//Here's where the magic happens
annotationView.image=[UIImage imageNamed:#"apple.gif"];
annotationView.canShowCallout = YES;
}
return annotationView;
}
return nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I see that the tutorial has the MyAnnotationClass interface as:
#interface MyAnnotationClass : NSObject
When I used MKAnnotation I set my annotation interface up as:
#interface MyAnnotationClass : MKAnnotationView <MKAnnotation>
Then in the MyAnnotationClass.m file I have the following methods:
- (NSString *)title{
return self.name;
}
- (NSString *)subtitle{
return self.description;
}
Can anyone help me with a problem I have with a mapView application that is only displaying the default location and not the specific location I have specified. My code is as follows. I'm learning Xcode so have probably made a schoolboy error :-
.H
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface RavenMapViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
-(void) goLocation;
#end
.M
#import "RavenMapViewController.h"
#implementation RavenMapViewController
#synthesize mapView;
-(void) goLocation
{
MKCoordinateRegion newRegion;
newRegion.center.latitude = 39.278112;
newRegion.center.longitude = -76.622772;
newRegion.span.latitudeDelta = 0.008388;
newRegion.span.longitudeDelta = 0.016243;
[self.mapView setRegion:newRegion animated:YES];
}
- (void)viewDidLoad
{
[self goLocation];
}
- (void)viewDidUnload
{
}
#end
I've boiled a very complex set of web services and searches down to the simple following code.
I need to be able to add annotations to a map in response to a search (or in the sample below to the click of a button), then allow the user to click the button again and get a new set of results. In reality there will be a different number, but in the simplified example, we always add one annotation to the mapview.
I believe my code should remove the existing annotations and add a new one, but it leaks 32 bytes on second and subsequent button pushes.
What am I missing ? (Or retaining as the case may be!)
testViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MyMark.h"
#interface testViewController : UIViewController {
MKMapView *mapView;
}
#end
testViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title=#"test";
}
return self;
}
- (void) storeLocationInfo: (CLLocationCoordinate2D) loc title:(NSString *)t subtitle:(NSString *)st index:(int)i {
NSArray * annotations = [mapView annotations];
[mapView removeAnnotations:annotations];
MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc];
[mapView addAnnotation:mymark];
[MyMark release];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"Add point to map" style:UIBarButtonItemStylePlain target:self action:#selector(addPushed)];
[self.navigationItem setRightBarButtonItem:barButton];
[barButton release];
mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
mapView.showsUserLocation=FALSE;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];
[mapView release];
}
- (void) addPushed {
MKCoordinateRegion reg = mapView.region;
[self storeLocationInfo:reg.center title:#"price" subtitle:#"title" index:1];
}
- (void)dealloc {
[super dealloc];
}
MyMark.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
int index;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly) int index;
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSString * subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
-(id)setCoordinate:(CLLocationCoordinate2D) coordinate;
-(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int)i ;
#end
MyMark.m
#import "MyMark.h"
#implementation MyMark
#synthesize coordinate, index;
#synthesize title,subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
-(id)setCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
-(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int) i{
self.title=t;
self.subtitle=st;
index=i;
return self;
}
-(void) dealloc {
[title release];
[subtitle release];
[super dealloc];
}
You're not releasing mymark in storeLocationInfo:title:subtitle:index:. It looks like the problem is a typing error. The line that reads
[MyMark release];
should be
[mymark release];
Note the case difference. The first line sends release to the class, not the instance.