The Pin is not working - ios

My Map shows a pin on a specific place but it's not showing the pin
Here is the code
WadiRumViewControllerJordan.h
#import <UIKit/UIKit.h>
#include <MapKit/MapKit.h>
#interface WadiRumViewControllerJordan : UIViewController
#property (strong, nonatomic) IBOutlet MKMapView *WadiRumMapView;
#end
WadiRumViewControllerJordan.m
#import "WadiRumViewControllerJordan.h"
#import "WadiRumNSOjectPIN.h"
#interface WadiRumViewControllerJordan ()
#end
//Wadi Rum Coordinates
#define WadiRum_Latitude 29.537355
#define WidiRum_longtitude 35.415026
//Wadi Rum Span
#define WadiRumSpan 0.01f;
#implementation WadiRumViewControllerJordan
#synthesize WadiRumMapView;
- (void)viewDidLoad {
[super viewDidLoad];
//Create WadiRum Region
MKCoordinateRegion WadiRumRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = WadiRum_Latitude;
center.longitude = WidiRum_longtitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta = WadiRum_Latitude;
span.longitudeDelta = WidiRum_longtitude;
WadiRumRegion.center = center;
WadiRumRegion.span = span;
//Set our map
[WadiRumMapView setRegion:WadiRumRegion animated:YES];
//WadiRumNSObjectPIN
//1. Create a coordinate for the use of WadiRum
CLLocationCoordinate2D WadiRumLocation;
WadiRumLocation.latitude = WadiRum_Latitude;
WadiRumLocation.longitude = WidiRum_longtitude;
WadiRumNSOjectPIN * WadiRumAnnitation = [[WadiRumNSOjectPIN alloc] init];
WadiRumAnnitation.coordinate = WadiRumLocation;
WadiRumAnnitation.title = #"Services";
WadiRumAnnitation.subtitle = #"Desert";
{[self.WadiRumMapView addAnnotation:WadiRumAnnitation];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be WadiRumNSOjectPIN
}
#end
WadiRumNSOjectPIN.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface WadiRumNSOjectPIN : NSObject <MKAnnotation>
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString * title;
#property(nonatomic, copy) NSString * subtitle;
#end
WadiRumNSOjectPIN.m
#import "WadiRumNSOjectPIN.h"
#implementation WadiRumNSOjectPIN
#synthesize coordinate;
- (id)initWithLocation:(CLLocationCoordinate2D)coord {
self = [super init];
if (self) {
coordinate = coord;
}
return self;
}
#synthesize coordinate, title, subtitle;
#end
I edited the code above to make it exactly like what I want, I got this error in the picture bellow

In order to conform to MKAnnotation, you must have properties called coordinate, title and subtitle. You've added three extra properties, ttcoordinate, tttitle, and ttsubtitle, but MKAnnotation is going to ignore those, and will look for coordinate, title, and subtitle.
The key reason you're not seeing your annotation is that you're setting ttcoordinate in viewDidLoad. But MKAnnotation will not use that, but rather will refer to the coordinate property you synthesized, but never set. (You do have an initWithLocation method, which suggests you were going to update coordinate, but you never call that.)
Bottom line, I would suggest renaming ttcoordinate, ttitle and ttsubtitle to coordinate, title, and subtitle, and updating all of those references accordingly, and that should fix everything. And you can retire the #synthesize line.

Related

mapview autozoom on current location automatic

I have one problem. I've been looking at the other answers here on stack overflow about the same question and I can't get them to work for me. So I'm going to try asking here. What I want to do is when I'm on map view and have got the user location I want to automatically zoom into the location.
the h-file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface WalkingTableViewController: UIViewController <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *MKMapView;
#end
the m-file
#import "WalkingTableViewController.h"
#interface UITableViewController ()
#end
#implementation WalkingTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.MKMapView.showsUserLocation=YES;
self.MKMapView.delegate = self;
[self.MKMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
- (IBAction)StopButton:(id)sender
- (IBAction)StartButton:(id)sender
#end
If you like to see the zoom animated, I would place the code in the viewDidAppear method, so that the animation starts once the view controller is displayed.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
MKCoordinateRegion tRegion = MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate], 20000, 20000);
[self.MKMapView setRegion:tRegion animated:animated];
}
Adjust the region (zoom level, 20000 in the example above) to your needs.
What you have to do is tell the mapview to zoom into a region. This should get you started:
CLLocationCoordinate2D center = _mapView.userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMake(center, MKCoordinateSpanMake(2000, 2000));
[_mapView setRegion:region animated:YES];

MKMapView does not read the given coordinates

ViewController.h
#import <UIKit/UIKit.h>
#import "MapKit/MapKit.h"
#interface ViewController : UIViewController <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Setting Region
//MKCoordinateRegion *myRegion;
//Center
CLLocationCoordinate2D center;
center.longitude = 55.316667;
center.latitude = 25.266667;
//SPAN
MKCoordinateSpan span;
span.latitudeDelta = 0.30f;
span.longitudeDelta = 0.30f;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
As you see my code seems to be fine, and normally it should show the location given in latitude and longtitude values. But instead it shows this :
I am working in Xcode 5 with iOS 6.
Any ideas about the problem?
Nowhere in the code shown does it "give the coordinates to the map view" so why should it do anything?
All the code does is:
Declares local variables called center and span in the viewDidLoad method.
Sets the values of these local variables (which the map view knows nothing about).
The viewDidLoad method ends (doing nothing with the values of the local variables).
Adding this line after setting center and span actually gives the coordinates to the map view so that it shows those coordinates:
mapView.region = MKCoordinateRegionMake(center, span);
(Assuming, of course, that the mapView IBOutlet is connected to the MKMapView in the xib.)
Now, this will center the map view at the coordinates you specified and that's it.
If you were expecting any other effects (such as a pin appearing at the coordinates), you'll need to create an annotation with those coordinates and tell the map view to add it.

MKMapView is not initialized

I am doing some unit tests on a ViewController that has an MKMapView.
If I want to test if the mapview is not nil, I have to explicitly initialize it, which is unnecessary to otherwise just use the mapview.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface HomeVC : UIViewController <UIImagePickerControllerDelegate, CLLocationManagerDelegate, MKMapViewDelegate> {
MKMapView* mapView;
CLLocationManager* locationManager;
CLLocationCoordinate2D currentLocation;
}
#property (nonatomic, strong) IBOutlet MKMapView* mapView;
#property(nonatomic, strong)CLLocationManager* locationManager;
#property(nonatomic)CLLocationCoordinate2D currentLocation;
#end
#implementation HomeVC
#synthesize mapView, cameraButton,cameraView, locationManager, currentLocation;
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView = MKMapView.new;
self.mapView.mapType = MKMapTypeStandard;
}
If I put a breakpoint after the map initialization, it shows the mapView initialized with only a UIView property. It is properly hooked up in my Storyboard file. And works fine, I just can't test any properties on it.

Re-using this code for multiple mapkit pin annotations

I have this code below working perfectly for a unique Pin and one annotation. I want to adapt it without too many changes to display more Pins, locations and annotations.
There is a class called MyAnnotationPins with the following lines:
MyAnnotationPins.h
#interface MyAnnotationPins : NSObject < MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly, copy) NSString *title;
#property (nonatomic, readonly, copy) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle;
MyAnnotationPins.m
#synthesize coordinate;
#synthesize subtitle;
#synthesize title;
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle
{
self = [super init];
if (self)
{
coordinate = annotCoordinate;
subtitle = [[NSString alloc] initWithString:annotSubtitle];
title = [[NSString alloc] initWithString:annotTitle];
}
return self;
}
And at the view controller the following code:
SecondViewController.h
import "MyAnnotationPins.h"
#interface SecondViewController : UIViewController < MKMapViewDelegate >
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) MyAnnotationPins* biblioAnnotation;
At the SecondViewController.m the implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
MKCoordinateRegion mapRegion;
mapRegion.center.latitude=-18.924129;
mapRegion.center.longitude=-48.283963;
mapRegion.span.latitudeDelta=0.2;
mapRegion.span.longitudeDelta=0.2;
[mapView setRegion:mapRegion animated:YES];
///// This is just for One annotaion/Pin on Map /////
CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963);
biblioAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:parliamentLocation
title:#"Ponto Biblioteca"
subtitle:#"Taxi proximo"];
[mapView addAnnotation:biblioAnnotation];
If you want more than one pin and annotaion copy the CLLocation instance below and change the following atributes
CLLocationCoordinate2D secondLocation = CLLocationCoordinate2DMake(another latitude, another longitude);
secondAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:secondLocation
title:#"Second Title"
subtitle:#"Second subtitle"];
[mapView addAnnotation:secondAnnotation]; <code>
And so on for the third, fourth fifth etc. Do not forget to create the secondLocation proerty at your view controller like the first one in SecondViewController.h and also
#synthesize secondAnnotation property at SecondViewController.m file
#property (strong, nonatomic) MyAnnotationPins* secondAnnotation;
Add them in a loop like so
for(X in Y)
{
CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963);
MyAnnotationPins* biblioAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:parliamentLocation
title:#"Ponto Biblioteca"
subtitle:#"Taxi proximo"];
[mapView addAnnotation:biblioAnnotation];
}
As you loop through your list you can get the right coordinates and title for each one. This way you list can grow or shrink and you won't need to add third, fourth or fifth annotation properties.

Not able to initialize CLLocationCoordinate2D iVar in MKAnnotation Classe

I am trying to put MKAnnotations on MKMapView. I am following the procedure that has been suggested on various available tutorials. I have created PlaceMark.h/.m file from NSobject Class as following.
PlaceMark.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface PlaceMark : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
}
#property (nonatomic,copy) NSString *title;
#property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
#end
PlaceMark.m
#import "PlaceMark.h"
#implementation PlaceMark
#synthesize coordinate,title;
-(void)dealloc
{
[title release];
[super dealloc];
}
#end
in my viewController which holds MKMapView, in viewDidload I have following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKCoordinateRegion region;
region.center.latitude=37.3305262;
region.center.longitude=-122.0290935;
region.span.latitudeDelta=0.01;
region.span.longitudeDelta=0.01;
[parkingMap setRegion:region animated:YES];
PlaceMark *ann=[[[PlaceMark alloc]init]autorelease]; // I have also tired it using explicitly defining method -(id)initwithTitle.... but it did not worked.
ann.title=#"Test";
ann.coordinate= region.center;
[parkingMap addAnnotation:ann];
}
Can anyone please tell me what I am doing wrong? BTW I am using Xcode4.2 with iOS sdk5.0 and
Not using storyboards/automatic reference counting
Thanks
Sumit
You've defined the coordinate property as readonly so you can't set it.
Change it to readwrite.
Also, if you just need a basic annotation object with a title, subtitle, and settable coordinate, you can use the built-in MKPointAnnotation class instead of defining your own class. The creation and initialization would be identical (just replace your class name with MKPointAnnotation).

Resources