how to place location icon on mapkit objective c - ios

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

Related

Track a device and display data with annotations in IOS

I would like to store coordinates with additional data according to the gps locations and view the coordinates in a map with pin annotations and the additional data in Callouts (Title, Subtitle). Ive already managed to get my data from my device to my iPhone via Bluetooth. I get about every second new coordinates but my device is very slow, so i only need so save the coordinates and the additional data every 10 meters. Im new in iOS programming and Im looking forward to your help! ;)
Here is my Code:
ViewController.m
#interface SecondViewController ()
#end
extern float lat;
extern float lon;
#implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGradients];
_mapView.showsUserLocation = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (lat > 0) {
double pointOneLatitude = lat;
double pointOneLongitude = lon;
CLLocationCoordinate2D pointOneCoordinate = {pointOneLatitude, pointOneLongitude};
KTMapAnnotation *pointOneAnnotation = [[KTMapAnnotation alloc] initWithCoordinate:pointOneCoordinate];
[pointOneAnnotation setTypeOfAnnotation:PIN_ANNOTATION];
[self.mapView addAnnotation:pointOneAnnotation];
}
}
ViewController.h
import <UIKit/UIKit.h>
import <MapKit/MapKit.h>
import "KTMapAnnotation.h"
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
KTMapAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface KTMapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D _coordinate;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
// 08 - Add a Callout
- (NSString*) title;
- (NSString*) subtitle;
#property(nonatomic, strong) NSString *typeOfAnnotation;
#end
KTMapAnnotation.m
#import "SecondViewController.h"
#import "KTMapAnnotation.h"
#implementation KTMapAnnotation
#synthesize coordinate=_coordinate;
#synthesize typeOfAnnotation;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil)
{
_coordinate = coordinate;
}
return self;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
_coordinate = newCoordinate;
}
// Add a Callout
- (NSString*) title
{
return #"Title“;
}
// Add a Callout
- (NSString*) subtitle
{
return #„subtitel";
}
#end

MKAnnotation - How to display a button on the annotation bubble

I am trying to use a map to set annotation in it, where the user wants to put it.
I want the user to touch the screen where he wants to put a pin, and by clicking on the appeared pin, he can be redirected to another view where he can put details.
I followed several tutorials to make the button appear on the right of the callout, but this does not work...
Here is my code :
MAPpin is the NSObject file :
MAPpin.h :
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MAPpin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
MAPpin.m :
#import "MAPpin.h"
#implementation MAPpin
#synthesize coordinate,title,subtitle, mapView;
#end
And my view controller :
MAPwelcomeViewController.h
#import <UIKit/UIKit.h>
#import <MAPKit/MKAnnotation.h>
#import "MAPAppDelegate.h"
#import "MAPpin.h"
#interface MAPwelcomeViewController : UIViewController <MKMapViewDelegate>
- (IBAction)longpress:(UILongPressGestureRecognizer *)sender;
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
and the MAPwelcomeViewController.m :
#import "MAPwelcomeViewController.h"
#import "MAPpin.h"
#import "myAnnotation.h"
#define METERS_PER_MILE 1609.344
#interface MAPwelcomeViewController ()
#end
#implementation MAPwelcomeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
{
[super viewDidLoad];
_mapView.showsUserLocation = YES;
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//1
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 40.740848;
zoomLocation.longitude= -73.991145;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
[self.mapView setRegion:viewRegion animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark -MapView Delegate Methods
//6
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
//7
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
//8
static NSString *identifier = #"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
//9
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.animatesDrop = YES;
annotationView.canShowCallout = YES;
}else {
annotationView.annotation = annotation;
}
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
- (IBAction)longpress:(UILongPressGestureRecognizer *)sender {
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D loccoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
myAnnotation *ann = [[myAnnotation alloc] initWithCoordinate:loccoord title:#"Test"];
[self.mapView addAnnotation:ann];
}
#end
So following (http://www.codigator.com/tutorials/mapkit-tutorial-for-ios-beginners/"this links") I added the following method :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
But when I run the app, no button is showing... what do you think I am doing wrong ?
Thanks in advance,
regards !
Most likely, the map view's delegate is not set so the viewForAnnotation delegate method never gets called and the map view creates a default red pin with no callout accessory button.
In the storyboard, right-click on the map view control and connect its delegate outlet to the View Controller.
Or, in code, in the viewDidLoad method, before setting showsUserLocation, set the delegate programmatically:
self.mapView.delegate = self;
Some unrelated comments...
Once the button is appearing, to handle the user tapping the callout accessory, a recommended approach is to implement the calloutAccessoryControlTapped delegate method. See the answer to MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked for an example.
A separate issue I should point out is that the gesture handler method isn't checking the gesture recognizer's state before creating an annotation. So what can happen is multiple pins can get created while the user is still doing a single long press. To avoid this, add a check like this at the top of the longpress: method:
if (sender.state != UIGestureRecognizerStateBegan)
{
return;
}
Another unrelated thing is that the code is creating an annotation of type myAnnotation by doing myAnnotation alloc but the annotation class you've shown is of type MAPpin.
Finally, adding an MKMapView as an IBOutlet and property in the MAPpin class is completely unnecessary. You should remove it from there before it leads to confusion.

Can't set title on MKPinAnnotationView

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;
}

trying to pass text from tableview to a view with a map

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;

Removing MKMapView Annotations causes leaks

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.

Resources