I have just started to use MapKit framework, and my first practice did not go well :)
Everything seems to be works fine except MapView showing somewhere in ocean and it did not find any place (ex: Land, island). I think latitude and longitude getting out of range somehow.
here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *zoomButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-In"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomIn:)];
UIBarButtonItem *typeButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-Out"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomOut:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:zoomButton, typeButton, nil];
barButtons.items = buttons;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"My Company";
annotationPoint.subtitle = #"my Company Title";
//[self __mapView];
MKCoordinateRegion adjustedRegion = [__mapView regionThatFits:viewRegion];
[__mapView setRegion:adjustedRegion animated:YES];
[__mapView addAnnotation:annotationPoint];
}
You are setting zoomLocation coordinates after creating region. Try first to set zoomLocation
...
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
...
Related
How can I set the longitude and latitude using a textfield? I have tried making my textfield a double but it just seems to crash.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = *(myTextField);
region.center.longitude = *(myTextField1);
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
How I can make this work?
MKPointAnnotation *annonation = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mycordinate;
mycordinate.latitude = [self.latitude floatValue];
mycordinate.longitude =[self.longitude floatValue];
annonation.coordinate = mycordinate;
[self.mapview addAnnotation:annonation];
In self.latitude store your latitude value and
in self.longitude store your longitude value.
Try below Code:
Just get text from textFields and set latitude,longitude float value from text.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = [self.myTextField.text floatValue];
region.center.longitude = [self.myTextField1.text floatValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Test";
ann.subtitle = #"test";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
I'm trying to populate a detail disclosure but it won't generate. I don't have any errors or warnings when compiling and building the app except one here : "local declaration of mapView hides instance variable. I know what that means, it means that its already used elsewhere. and when i type self.webView the error goes away but the detail disclosure still doesn't populate. let me know what i'm doing wrong please:
Annotations:
MapViewAnnotion.h
#import
Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sub;
#end
MapViewAnnotation.m
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize title, coordinate,subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sub{
//[super init];
title = ttl;
coordinate = c2d;
subtitle = sub;
return self;
}
- (void)dealloc {
// [title release];
// [super dealloc];
}
#end
and then...
MapViewController.h
#import <UIKit/UIKit.h>
#import "RESideMenu.h"
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController
{
IBOutlet MKMapView *mapView;
IBOutlet UISegmentedControl *segmentedControl;
}
#property (nonatomic, retain) MKMapView *mapView;
#property (nonatomic, retain) UISegmentedControl *segmentedControl;
- (IBAction)segmentedControllChanged:(id)sender;
#end
MapViewController.m
#import "MapViewController.h"
#import "MapViewAnnotation.h"
#import "config.h"
#interface MapViewController ()
#end
#implementation MapViewController
#synthesize mapView, segmentedControl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = [NSString stringWithFormat:#"\tCurrently In : %#",[config getSubTitle]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(showMenu)];
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:(93/255.0) green:(149/255.0) blue:(197/255.0) alpha:1]];
// All map stuff
mapView.showsUserLocation = YES;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[config getTitle] andCoordinate:location andSubtitle:[config getSubTitle]];
[self.mapView addAnnotation:newAnnotation];
// Do any additional setup after loading the view from its nib.
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.99, .99);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
//!!! Map Points
// UNITED STATES
// Alaska
MKPointAnnotation *anchorage = [[MKPointAnnotation alloc] init];
anchorage.coordinate = CLLocationCoordinate2DMake(61.218056, -149.900278);
anchorage.title = #"Anchorage";
anchorage.subtitle = #"Alaska";
[mapView addAnnotation:anchorage];
MKPointAnnotation *chena = [[MKPointAnnotation alloc] init];
chena.coordinate = CLLocationCoordinate2DMake(64.88349, -146.855596);
chena.title = #"Chena Hot Springs";
chena.subtitle = #"Alaska";
[mapView addAnnotation:chena];
MKPointAnnotation *healy = [[MKPointAnnotation alloc] init];
healy.coordinate = CLLocationCoordinate2DMake(63.869738, -149.021511);
healy.title = #"Healy";
healy.subtitle = #"Alaska";
[mapView addAnnotation:healy];
MKPointAnnotation *palmer = [[MKPointAnnotation alloc] init];
palmer.coordinate = CLLocationCoordinate2DMake(61.599722, -149.112778);
palmer.title = #"Palmer";
palmer.subtitle = #"Alaska";
[mapView addAnnotation:palmer];
MKPointAnnotation *seward = [[MKPointAnnotation alloc] init];
seward.coordinate = CLLocationCoordinate2DMake(60.104167, -149.442222);
seward.title = #"Seward";
seward.subtitle = #"Alaska";
[mapView addAnnotation:seward];
MKPointAnnotation *wasilla = [[MKPointAnnotation alloc] init];
wasilla.coordinate = CLLocationCoordinate2DMake(61.581389, -149.439444);
wasilla.title = #"Wasilla";
wasilla.subtitle = #"Alaska";
[mapView addAnnotation:wasilla];
// Arizona
MKPointAnnotation *tucson = [[MKPointAnnotation alloc] init];
tucson.coordinate = CLLocationCoordinate2DMake(32.221743, -110.926479);
tucson.title = #"Tuscon";
tucson.subtitle = #"Arizona";
[mapView addAnnotation:tucson];
MKPointAnnotation *phoenix = [[MKPointAnnotation alloc] init];
phoenix.coordinate = CLLocationCoordinate2DMake(33.448377, -112.074037);
phoenix.title = #"Phoenix";
phoenix.subtitle = #"Arizona";
[mapView addAnnotation:phoenix];
// California
MKPointAnnotation *anaheim = [[MKPointAnnotation alloc] init];
anaheim.coordinate = CLLocationCoordinate2DMake(33.835293, -117.914504);
anaheim.title = #"Anaheim";
anaheim.subtitle = #"California";
[mapView addAnnotation:anaheim];
// Colorado
MKPointAnnotation *denver = [[MKPointAnnotation alloc] init];
denver.coordinate = CLLocationCoordinate2DMake(39.737567, -104.984718);
denver.title = #"Denver";
denver.subtitle = #"Colorado";
[mapView addAnnotation:denver];
// Georgia
MKPointAnnotation *augusta = [[MKPointAnnotation alloc] init];
augusta.coordinate = CLLocationCoordinate2DMake(33.473498, -82.010515);
augusta.title = #"Augusta";
augusta.subtitle = #"Georgia";
[mapView addAnnotation:augusta];
// Hawaii
MKPointAnnotation *honolulu = [[MKPointAnnotation alloc] init];
honolulu.coordinate = CLLocationCoordinate2DMake(21.306944, -157.858333);
honolulu.title = #"Honolulu";
honolulu.subtitle = #"Hawaii";
[mapView addAnnotation:honolulu];
// Indiana
MKPointAnnotation *terrehaute = [[MKPointAnnotation alloc] init];
terrehaute.coordinate = CLLocationCoordinate2DMake(39.466703, -87.413909);
terrehaute.title = #"Terre Haute";
terrehaute.subtitle = #"Indiana";
[mapView addAnnotation:terrehaute];
MKPointAnnotation *indianapolis = [[MKPointAnnotation alloc] init];
indianapolis.coordinate = CLLocationCoordinate2DMake(39.768403, -86.158068);
indianapolis.title = #"Indianapolis";
indianapolis.subtitle = #"Indiana";
[mapView addAnnotation:indianapolis];
// Michigan
MKPointAnnotation *westland = [[MKPointAnnotation alloc] init];
westland.coordinate = CLLocationCoordinate2DMake(42.324204, -83.400211);
westland.title = #"Westland";
westland.subtitle = #"Michigan";
[mapView addAnnotation:westland];
MKPointAnnotation *detroit = [[MKPointAnnotation alloc] init];
detroit.coordinate = CLLocationCoordinate2DMake(42.331427, -83.045754);
detroit.title = #"Detroit";
detroit.subtitle = #"Michigan";
[mapView addAnnotation:detroit];
// Missouri
MKPointAnnotation *stlouis = [[MKPointAnnotation alloc] init];
stlouis.coordinate = CLLocationCoordinate2DMake(38.627003, -90.199404);
stlouis.title = #"St Louis";
stlouis.subtitle = #"Missouri";
[mapView addAnnotation:stlouis];
// New York
MKPointAnnotation *newyork = [[MKPointAnnotation alloc] init];
newyork.coordinate = CLLocationCoordinate2DMake(40.712784, -74.005941 );
newyork.title = #"New York";
newyork.subtitle = #"New York";
[mapView addAnnotation:newyork];
MKPointAnnotation *manhattan = [[MKPointAnnotation alloc] init];
manhattan.coordinate = CLLocationCoordinate2DMake(40.790278, -73.959722);
manhattan.title = #"Manhattan";
manhattan.subtitle = #"New York";
[mapView addAnnotation:manhattan];
MKPointAnnotation *queens = [[MKPointAnnotation alloc] init];
queens.coordinate = CLLocationCoordinate2DMake(40.75, -73.866667);
queens.title = #"Queens";
queens.subtitle = #"New York";
[mapView addAnnotation:queens];
// North Carolina
MKPointAnnotation *asheville = [[MKPointAnnotation alloc] init];
asheville.coordinate = CLLocationCoordinate2DMake(35.595058, -82.551487);
asheville.title = #"Asheville";
asheville.subtitle = #"North Carolina";
[mapView addAnnotation:asheville];
MKPointAnnotation *butner = [[MKPointAnnotation alloc] init];
butner.coordinate = CLLocationCoordinate2DMake(36.132089, -78.756671);
butner.title = #"Butner";
butner.subtitle = #"North Carolina";
[mapView addAnnotation:butner];
MKPointAnnotation *creedmoor = [[MKPointAnnotation alloc] init];
creedmoor.coordinate = CLLocationCoordinate2DMake(36.122368, -78.686115);
creedmoor.title = #"Creedmoor";
creedmoor.subtitle = #"North Carolina";
[mapView addAnnotation:creedmoor];
MKPointAnnotation *charlotte = [[MKPointAnnotation alloc] init];
charlotte.coordinate = CLLocationCoordinate2DMake(35.227087, -80.843127);
charlotte.title = #"Charolette";
charlotte.subtitle = #"North Carolina";
[mapView addAnnotation:charlotte];
MKPointAnnotation *fayetteville = [[MKPointAnnotation alloc] init];
fayetteville.coordinate = CLLocationCoordinate2DMake(35.052664, -78.878358);
fayetteville.title = #"Fayetteville";
fayetteville.subtitle = #"North Carolina";
[mapView addAnnotation:fayetteville];
MKPointAnnotation *jacksonville = [[MKPointAnnotation alloc] init];
jacksonville.coordinate = CLLocationCoordinate2DMake(34.754052, -77.430241);
jacksonville.title = #"Jacksonville";
jacksonville.subtitle = #"North Carolina";
[mapView addAnnotation:jacksonville];
MKPointAnnotation *greenville = [[MKPointAnnotation alloc] init];
greenville.coordinate = CLLocationCoordinate2DMake(35.612661, -77.366354);
greenville.title = #"Greenville";
greenville.subtitle = #"North Carolina";
[mapView addAnnotation:greenville];
MKPointAnnotation *greensboro = [[MKPointAnnotation alloc] init];
greensboro.coordinate = CLLocationCoordinate2DMake(36.072635, -79.791975);
greensboro.title = #"Greensboro";
greensboro.subtitle = #"North Carolina";
[mapView addAnnotation:greensboro];
MKPointAnnotation *winston = [[MKPointAnnotation alloc] init];
winston.coordinate = CLLocationCoordinate2DMake(36.09986, -80.244216);
winston.title = #"Winston";
winston.subtitle = #"North Carolina";
[mapView addAnnotation:winston];
MKPointAnnotation *boone = [[MKPointAnnotation alloc] init];
boone.coordinate = CLLocationCoordinate2DMake(36.216795, -81.674552);
boone.title = #"Boone";
boone.subtitle = #"North Carolina";
[mapView addAnnotation:boone];
MKPointAnnotation *raleigh = [[MKPointAnnotation alloc] init];
raleigh.coordinate = CLLocationCoordinate2DMake(35.77959, -78.638179);
raleigh.title = #"Raleigh";
raleigh.subtitle = #"North Carolina";
[mapView addAnnotation:raleigh];
MKPointAnnotation *durham = [[MKPointAnnotation alloc] init];
durham.coordinate = CLLocationCoordinate2DMake(35.994033, -78.898619);
durham.title = #"Durham";
durham.subtitle = #"North Carolina";
[mapView addAnnotation:durham];
MKPointAnnotation *goldsboro = [[MKPointAnnotation alloc] init];
goldsboro.coordinate = CLLocationCoordinate2DMake(35.384884, -77.992765);
goldsboro.title = #"Goldsboro";
goldsboro.subtitle = #"North Carolina";
[mapView addAnnotation:goldsboro];
MKPointAnnotation *kinston = [[MKPointAnnotation alloc] init];
kinston.coordinate = CLLocationCoordinate2DMake(35.262664, -77.581635);
kinston.title = #"Kinston";
kinston.subtitle = #"North Carolina";
[mapView addAnnotation:kinston];
MKPointAnnotation *lagrange = [[MKPointAnnotation alloc] init];
lagrange.coordinate = CLLocationCoordinate2DMake(35.306829, -77.788034);
lagrange.title = #"La Grange";
lagrange.subtitle = #"North Carolina";
[mapView addAnnotation:lagrange];
MKPointAnnotation *oxford = [[MKPointAnnotation alloc] init];
oxford.coordinate = CLLocationCoordinate2DMake(36.3107, -78.590835);
oxford.title = #"Oxford";
oxford.subtitle = #"North Carolina";
[mapView addAnnotation:oxford];
MKPointAnnotation *morrisville = [[MKPointAnnotation alloc] init];
morrisville.coordinate = CLLocationCoordinate2DMake(35.823483, -78.825562);
morrisville.title = #"Morrisville";
morrisville.subtitle = #"North Carolina";
[mapView addAnnotation:morrisville];
MKPointAnnotation *roxboro = [[MKPointAnnotation alloc] init];
roxboro.coordinate = CLLocationCoordinate2DMake(36.393752, -78.982788);
roxboro.title = #"Roxboro";
roxboro.subtitle = #"North Carolina";
[mapView addAnnotation:roxboro];
MKPointAnnotation *henderson = [[MKPointAnnotation alloc] init];
henderson.coordinate = CLLocationCoordinate2DMake(36.329591, -78.399164);
henderson.title = #"Henderson";
henderson.subtitle = #"North Carolina";
[mapView addAnnotation:henderson];
MKPointAnnotation *chapelhill = [[MKPointAnnotation alloc] init];
chapelhill.coordinate = CLLocationCoordinate2DMake(35.9132, -79.055845);
chapelhill.title = #"Chapel Hill";
chapelhill.subtitle = #"North Carolina";
[mapView addAnnotation:chapelhill];
MKPointAnnotation *apex = [[MKPointAnnotation alloc] init];
apex.coordinate = CLLocationCoordinate2DMake(35.732652, -78.850286);
apex.title = #"Apez";
apex.subtitle = #"North Carolina";
[mapView addAnnotation:apex];
// Oklahoma
MKPointAnnotation *lawton = [[MKPointAnnotation alloc] init];
lawton.coordinate = CLLocationCoordinate2DMake(34.603567, -98.395929);
lawton.title = #"Lawton";
lawton.subtitle = #"Oklahoma";
[mapView addAnnotation:lawton];
// Pennsylvania
MKPointAnnotation *mercersburg = [[MKPointAnnotation alloc] init];
mercersburg.coordinate = CLLocationCoordinate2DMake(39.82787, -77.903331);
mercersburg.title = #"Mercersburg";
mercersburg.subtitle = #"Pennsylvania";
[mapView addAnnotation:mercersburg];
// South Carolina
MKPointAnnotation *columbia = [[MKPointAnnotation alloc] init];
columbia.coordinate = CLLocationCoordinate2DMake(34.00071, -81.034814);
columbia.title = #"Columbia";
columbia.subtitle = #"South Carolina";
[mapView addAnnotation:columbia];
MKPointAnnotation *greenville2 = [[MKPointAnnotation alloc] init];
greenville2.coordinate = CLLocationCoordinate2DMake(34.852618, -82.39401);
greenville2.title = #"Greenville";
greenville2.subtitle = #"South Carolina";
[mapView addAnnotation:greenville2];
MKPointAnnotation *mbeach = [[MKPointAnnotation alloc] init];
mbeach.coordinate = CLLocationCoordinate2DMake(33.68906, -78.886694);
mbeach.title = #"Mrytle Beach";
mbeach.subtitle = #"South Carolina";
[mapView addAnnotation:mbeach];
// Tennessee
MKPointAnnotation *chattanooga = [[MKPointAnnotation alloc] init];
chattanooga.coordinate = CLLocationCoordinate2DMake(35.04563, -85.30968);
chattanooga.title = #"Chattanooga";
chattanooga.subtitle = #"Tennessee";
[mapView addAnnotation:chattanooga];
MKPointAnnotation *manchester = [[MKPointAnnotation alloc] init];
manchester.coordinate = CLLocationCoordinate2DMake(35.481743, -86.088599);
manchester.title = #"Manchester";
manchester.subtitle = #"Tennessee";
[mapView addAnnotation:manchester];
MKPointAnnotation *nashville = [[MKPointAnnotation alloc] init];
nashville.coordinate = CLLocationCoordinate2DMake(36.166667, -86.783333);
nashville.title = #"Nashville";
nashville.subtitle = #"Tennessee";
[mapView addAnnotation:nashville];
MKPointAnnotation *knoxville = [[MKPointAnnotation alloc] init];
knoxville.coordinate = CLLocationCoordinate2DMake(35.960638, -83.920739);
knoxville.title = #"Knoxville";
knoxville.subtitle = #"Tennessee";
[mapView addAnnotation:knoxville];
// Washington D.C.
MKPointAnnotation *wdc = [[MKPointAnnotation alloc] init];
wdc.coordinate = CLLocationCoordinate2DMake(38.907192, -77.036871);
wdc.title = #"Washington D.C.";
[mapView addAnnotation:wdc];
// Virgina
MKPointAnnotation *alexandria = [[MKPointAnnotation alloc] init];
alexandria.coordinate = CLLocationCoordinate2DMake(38.804836, -77.046921);
alexandria.title = #"Alexandria";
alexandria.subtitle = #"Virgina";
[mapView addAnnotation:alexandria];
MKPointAnnotation *arlington = [[MKPointAnnotation alloc] init];
arlington.coordinate = CLLocationCoordinate2DMake(38.87997, -77.10677);
arlington.title = #"Arlington";
arlington.subtitle = #"Virgina";
[mapView addAnnotation:arlington];
MKPointAnnotation *fairfax = [[MKPointAnnotation alloc] init];
fairfax.coordinate = CLLocationCoordinate2DMake(38.846224, -77.306373);
fairfax.title = #"Fairfax";
fairfax.subtitle = #"Virgina";
[mapView addAnnotation:fairfax];
MKPointAnnotation *williamsburg = [[MKPointAnnotation alloc] init];
williamsburg.coordinate = CLLocationCoordinate2DMake(37.270702, -76.707457);
williamsburg.title = #"Williamsburg";
williamsburg.subtitle = #"Virgina";
[mapView addAnnotation:williamsburg];
// OCONUS
// ASIA
// SOUTH KOREA
MKPointAnnotation *seoul = [[MKPointAnnotation alloc] init];
seoul.coordinate = CLLocationCoordinate2DMake(37.566535, 126.977969);
seoul.title = #"Seoul";
seoul.subtitle = #"South Korea";
[mapView addAnnotation:seoul];
MKPointAnnotation *suwon = [[MKPointAnnotation alloc] init];
suwon.coordinate = CLLocationCoordinate2DMake(37.263573, 127.028601);
suwon.title = #"Suwon";
suwon.subtitle = #"South Korea";
[mapView addAnnotation:suwon];
MKPointAnnotation *incheon = [[MKPointAnnotation alloc] init];
incheon.coordinate = CLLocationCoordinate2DMake(37.456256, 126.705206);
incheon.title = #"Incheon";
incheon.subtitle = #"South Korea";
[mapView addAnnotation:incheon];
MKPointAnnotation *sokcho = [[MKPointAnnotation alloc] init];
sokcho.coordinate = CLLocationCoordinate2DMake(38.207015, 128.591849);
sokcho.title = #"Sokcho-si";
sokcho.subtitle = #"South Korea";
[mapView addAnnotation:sokcho];
MKPointAnnotation *osan = [[MKPointAnnotation alloc] init];
osan.coordinate = CLLocationCoordinate2DMake(37.14981, 127.077221);
osan.title = #"Osan";
osan.subtitle = #"South Korea";
[mapView addAnnotation:osan];
MKPointAnnotation *pyeongtaek = [[MKPointAnnotation alloc] init];
pyeongtaek.coordinate = CLLocationCoordinate2DMake(36.992108, 127.112945);
pyeongtaek.title = #"Pyeongtaek-si";
pyeongtaek.subtitle = #"South Korea";
[mapView addAnnotation:pyeongtaek];
// THAILAND
MKPointAnnotation *bangkok = [[MKPointAnnotation alloc] init];
bangkok.coordinate = CLLocationCoordinate2DMake(13.727896, 100.524123);
bangkok.title = #"Bangkok";
bangkok.subtitle = #"Thailand";
[mapView addAnnotation:bangkok];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
return pinView;
}
- (void)showMenu
{
[self.sideMenuViewController presentMenuViewController];
}
- (IBAction)segmentedControllChanged:(id)sender {
if (segmentedControl.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}else if (segmentedControl.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeHybrid;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.199, .199);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
} else if (segmentedControl.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeSatellite;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.299, .299);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
any help is welcome :) Thank you
i am working on a map kit that shows all the data extracted from an xml web service .this web service contains 3 variables longitude (lon),latitude(lat), the name of the atm (atmName).the for loop is extracting the data from the Aatm array but the pins are not showing on the map .also my pins are all from the same region thats why i used a default center from the Aatm array with a default value. here is my code
thank you for helping me in advance :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Aatm = [[NSMutableArray alloc] init];
pinArray = [[NSMutableArray alloc] init];
NSString *url=#"http://192.168.83.1:8080/jeeRestApp-1.0/rest/Atm";
NSXMLParser *parser;
parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
parser.delegate=self;
if ([parser parse]==FALSE){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Erreur" message:#"erreur de connection!" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
//Setting the approximate region
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapview setShowsUserLocation:YES];
MKCoordinateRegion Myregion;
CLLocationCoordinate2D center1;
//replace by variable -----------------------------------
atm=[Aatm objectAtIndex:1];
center1.latitude=[atm.lat doubleValue];
center1.longitude=[atm.lon doubleValue];
NSLog(#"la latitude (defaut) est: %#",atm.lat);
MKCoordinateSpan span1;
span1.longitudeDelta = 0.01f;
span1.latitudeDelta = 0.01f;
Myregion.center=center1;
Myregion.span=span1;
[mapview setRegion:Myregion animated:YES];
for(int i = 0; i<=[Aatm count] - 1;i++)
{
//CLLocationCoordinate2D newCoord = { atm.lat, atm.lon};
atm=[Aatm objectAtIndex:i];
CLLocationCoordinate2D pinLocaton;
pinLocaton.latitude=[atm.lat doubleValue];
NSLog(#"latitude est de :%#",atm.lat);
pinLocaton.longitude=[atm.lon doubleValue];
NSLog(#"longitude est de :%#",atm.lon);
MKPointAnnotation *annotation=[MKPointAnnotation alloc];
annotation.title=atm.atmName;
NSLog(#"latitude est de :%#",atm.atmName);
annotation.coordinate=pinLocaton;
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation1"];
newAnnotation.pinColor = MKPinAnnotationColorGreen;
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = NO;
[newAnnotation setSelected:YES animated:YES];
[mapview addAnnotation:annotation];
NSLog(#"annotation added");
}
You need to implement mapView:viewForAnnotation: something like:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKPinAnnotationView* view = (MKPinAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:#"pin"];
if(!view)
{
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
}
else
{
view.annotation = annotation;
}
view.pinColor = MKPinAnnotationColorGreen;
view.animatesDrop = YES;
view.canShowCallout = NO;
return view;
}
else
{
return nil;
}
}
i want the map to get the location of the user and he can change the location by taping on the map. The problem is when tap on location the method (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation , called and the map show both the current location and the location he tapped in!! i want only one location >>>
here is the code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[mapView addGestureRecognizer:singleTap];
}
- (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation:(MKUserLocation *)userLocation
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", userLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", userLocation.coordinate.latitude];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[mapView addAnnotation:point];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
CLLocationCoordinate2D coord = [mapView convertPoint:[sender locationInView:mapView] toCoordinateFromView:mapView];
NSLog(#"Map touched %f, %f.", coord.latitude, coord.longitude);
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", coord.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", coord.latitude];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = coord;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[mapView removeAnnotations:[mapView annotations]];
[mapView addAnnotation:point];
}
Firstly, on ask one question on each page. If I answer question 2 right and someone else answers question 1 right, which one gets marked as the right answer?
Secondly, the method didUpdateUserLocation is called by iOS when it gets new information about where the device is. You don't need to add a new annotation there or you'll end up with lots of annotations for every time the device moves. If you want to show the user's current location call mapView.showsUserLocation = YES;. Once the user has tapped and you've created a new annotation, you can turn the userLocation off by setting it to NO.
I successfully implemented MKMapVIew and a single annotation on my Map. I am not able to represent two postitions simultaneously. I am using MKMapViewDelegate method :
mapView:viewForAnnotation:
Can someone look into this thing.
Thanks!
EDIT
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 22.569722 ;
region.center.longitude = 88.369722;
region.span.longitudeDelta = 0.1f;
region.span.latitudeDelta = 0.1f;
MKCoordinateRegion anotherRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
anotherRegion.center.latitude = 28.38 ;
anotherRegion.center.longitude = 77.12;
anotherRegion.span.longitudeDelta = 90.0f;
anotherRegion.span.latitudeDelta = 90.0f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #" Kolkata";
ann.subtitle = #"Mahatma Gandhi Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
DisplayAnotherMap *annMap = [[DisplayAnotherMap alloc] init];
annMap.title = #" New Delhi";
annMap.subtitle = #"Shahdara";
annMap.coordinate = anotherRegion.center;
[mapView addAnnotations:[NSArray arrayWithObjects:annMap,ann,nil]];
}
This will fulfill the requirement for you! ...:)
The method mapView:viewForAnnotation: is just for the view of a the annotations, eg. the colour of the pin and the title label etc. If you want to show multiple annotations you should alloc and init the all with the required positions. For example, you could create a .plist with all the coordinates and the in for cycle just add them.
EDIT
This is a sample code, taken from somewhere. You must alloc and init all anotations.
-(void)loadDummyPlaces{
srand((unsigned)time(0));
NSMutableArray *tempPlaces=[[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<1000; i++) {
MyPlace *place=[[MyPlace alloc] initWithCoordinate:CLLocationCoordinate2DMake([self RandomFloatStart:42.0 end:47.0],[self RandomFloatStart:14.0 end:19.0])];
[place setCurrentTitle:[NSString stringWithFormat:#"Place %d title",i]];
[place setCurrentSubTitle:[NSString stringWithFormat:#"Place %d subtitle",i]];
[place addPlace:place];
[tempPlaces addObject:place];
[place release];
}
places=[[NSArray alloc] initWithArray:tempPlaces];
[tempPlaces release];
}