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];
}
Related
In my browse view controller, the Map Kit annotations show up.
_mapVC = [[CollectionMapViewController alloc] init];
mapVC.collectedLeafArray = [collectionFetchedResultsController fetchedObjects];
mapVC.canShowCallout = YES;
mapVC.delegate = self;
[mapVC layoutMapView];
[self addChildViewController:mapVC];
[self.view insertSubview:mapVC.view belowSubview:_userCollectionSortMenu.view];
[mapVC didMoveToParentViewController:self];
I set up my child view controller the exact same way in my note taking view controller. However, they annotation pins don't show up.
_mapVC = [[CollectionMapViewController alloc] init];
_mapVC.collectedLeafArray = [NSArray arrayWithObject:_collectedLeaf];
_mapVC.delegate = self;
_mapVC.canShowCallout = NO;
[_mapVC layoutMapView];
[self addChildViewController:_mapVC];
[_scrollView addSubview:_mapVC.view];
[_mapVC didMoveToParentViewController:self];
I know the collected leaf is not nil, because I check the latitude in the CollectionMapViewController method that sets the annotations:
- (void)layoutMapView
{
NSMutableArray* leavesWithValidGeotag = [[NSMutableArray alloc] initWithCapacity:[collectedLeafArray count]];
for (CollectedLeaf* collectedLeaf in collectedLeafArray)
{
NSLog(#"latitude: %#", collectedLeaf.latitude);
if ( ![collectedLeaf.latitude isEqualToString:kGeoLocationNotAvailable] )
{
[leavesWithValidGeotag addObject:collectedLeaf];
LeafAnnotation* annotation = [[LeafAnnotation alloc] initWithLeaf:collectedLeaf];
[mapView addAnnotation:annotation];
[annotation release];
}
}
//// Adjust the region
if([leavesWithValidGeotag count] > 1)
{
NSArray* westEastSort = [leavesWithValidGeotag sortedArrayUsingSelector:#selector(longitudeCompare:)];
CollectedLeaf* eastMostLeaf = [westEastSort objectAtIndex:0];
CollectedLeaf* westMostLeaf = [westEastSort lastObject];
NSArray* southNorthSort = [leavesWithValidGeotag sortedArrayUsingSelector:#selector(latitudeCompare:)];
CollectedLeaf* southMostLeaf = [southNorthSort objectAtIndex:0];
CollectedLeaf* northMostLeaf = [southNorthSort lastObject];
CLLocationCoordinate2D center;
center.longitude = ([westMostLeaf.longitude doubleValue] + [eastMostLeaf.longitude doubleValue]) / 2.0f;
center.latitude = ([southMostLeaf.latitude doubleValue] + [northMostLeaf.latitude doubleValue]) / 2.0f;
MKCoordinateSpan span = MKCoordinateSpanMake(1.5 * fabs([northMostLeaf.latitude doubleValue] - [southMostLeaf.latitude doubleValue]),
1.5 * fabs([westMostLeaf.longitude doubleValue] - [eastMostLeaf.longitude doubleValue]));
if ( span.latitudeDelta < kMinimumSpan )
{
span.latitudeDelta = kMinimumSpan;
}
if ( span.longitudeDelta < kMinimumSpan )
{
span.longitudeDelta = kMinimumSpan;
}
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region];
}
else if([leavesWithValidGeotag count] == 1)
{
CollectedLeaf* theLeaf = [leavesWithValidGeotag objectAtIndex:0];
CLLocationCoordinate2D center;
center.longitude = [theLeaf.longitude doubleValue];
center.latitude = [theLeaf.latitude doubleValue];
MKCoordinateSpan span = MKCoordinateSpanMake(kMinimumSpan, kMinimumSpan);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region];
}
[leavesWithValidGeotag release];
}
Maybe, you need to copy NSArray object.
NSArray *test1 = [NSArray arrayWithObject:_collectedLeaf];
_mapVC.collectedLeafArray = [test1 copy];
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 have a MapView in which I would like to add annotations and a route from defined coordinates that I add from a Textfield and store in an NSMutableArray.
Now I'm able to show the route from multiple coordinates but only when I insert them in my code as follow :
-(void)loadMap{
int Coordinates;
//MAP
CLLocationCoordinate2D coordinateArray[Coordinates];
coordinateArray[0] = CLLocationCoordinate2DMake(LatA, LongA);
coordinateArray[1] = CLLocationCoordinate2DMake(LatB, LongB);
coordinateArray[2] = CLLocationCoordinate2DMake(LatC, LongC);
coordinateArray[3] = CLLocationCoordinate2DMake(LatD, LongD);
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:Coordinates];
[MapViewHome setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
[MapViewHome addOverlay:self.routeLine];
MapViewHome.mapType = MKMapTypeHybrid;
[self zoomToFitMapAnnotations:MapViewHome];
}
To Add Annotations I do this:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine)
{
if(nil == self.routeLineView)
{
self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
self.routeLineView.fillColor = [UIColor purpleColor];
self.routeLineView.strokeColor = [UIColor purpleColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;
}
-(void)AddAnotations{
DeparturePoint = [[MKPointAnnotation alloc] init];
DeparturePoint.coordinate = CLLocationCoordinate2DMake(LatA, LongA);
DeparturePoint.title = [NSString stringWithFormat:#"A"];
[MapViewHome addAnnotation:DeparturePoint];
ArrivalPoint = [[MKPointAnnotation alloc] init];
ArrivalPoint.coordinate = CLLocationCoordinate2DMake(LatB, LongB);
ArrivalPoint.title = [NSString stringWithFormat:#"B"];
[MapViewHome addAnnotation:ArrivalPoint];
C = [[MKPointAnnotation alloc] init];
C.coordinate = CLLocationCoordinate2DMake(LatC, LongC);
C.title = [NSString stringWithFormat:#"C"];
[MapViewHome addAnnotation:C];
D = [[MKPointAnnotation alloc] init];
D.coordinate = CLLocationCoordinate2DMake(LatD, LongD);
D.title = [NSString stringWithFormat:#"D"];
[MapViewHome addAnnotation:D];
}
NOW I would like to get Insert my Dynamic NSMutableArray in the LoadMap function in order to refresh the mapView and get a longer Route ! Any Idea ?
Here's the first solution I could think of...
First we wrap the CLLocationCoordinate2D into an object. For this I've made a wrapper class called KBLocationWrapper. Here's the interface:
#interface KBLocationWrapper : NSObject
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#end
Next generate the NSMutableArray ...
NSMutableArray *locationCoordinatesArray = [NSMutableArray array];
Then add each coordinate to the array via the object wrapper...
KBLocationWrapper *locationWrapper = [[KBLocationWrapper alloc] init];
locationWrapper.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[locationCoordinatesArray addObject:locationWrapper];
Finally, figure out how you're going to get the locationCoordinatesArray into the -loadMap method, and then loop through each object and map the coordinate property to its respective place in coordinateArray... (I would write a separate method for this functionality, but for demonstration purposes it's going straight into -loadMap)
-(void)loadMap{
....
int Coordinates = (int)[locationCoordinatesArray count];
CLLocationCoordinate2D coordinateArray[Coordinates];
// loop through coordinates
for (int i = 0; i < Coordinates; ++i) {
// write data from the CLLocationCoordinate2D stored in the wrapper
// to the primitive data array 'coordinateArray'
coordinateArray[i] = [locationCoordinatesArray[i] coordinate];
}
// then generate the routeLine.
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:Coordinates];
...
}
I add an object to NSMutableArray "NSLat" when i Unwind from another ViewController:
- (IBAction)UnwindPoiint:(UIStoryboardSegue *)segue {
float latitude;
float longitude;
NSString*PointName;
NSString*coordinates;
AddPointViewController *messageViewController = segue.sourceViewController;
PointName = messageViewController.PointBack;
latitude = [messageViewController.PointLatitude floatValue];
longitude = [messageViewController.PointLongitude floatValue];
KBLocationWrapper *locationWrapper = [[KBLocationWrapper alloc] init];
locationWrapper.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[NSLat addObject:locationWrapper];
coordinates = [NSString stringWithFormat:#"%f,%f,%#",latitude,longitude,PointName];
// [NSLat addObject:coordinates];
[self AddAnotations];
[self loadMap];
[FlightLogTable reloadData];
NSLog(#"%#",coordinates);
NSLog(#"%lu",(unsigned long)NSLat.count);
}
And I add Annotations & load Map as filed :
-(void)AddAnotations{
for(int idx = 0; idx < NSLat.count; idx++)
{
// break the string down even further to latitude and longitude fields.
NSString* currentPointString = [NSLat objectAtIndex:idx];
NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#","]];
CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
NSString*Name = [NSString stringWithFormat:#"%#",[latLonArr objectAtIndex:2]];
DeparturePoint = [[MKPointAnnotation alloc] init];
DeparturePoint.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
DeparturePoint.title = Name;
[MapViewHome addAnnotation:DeparturePoint];
}
[self loadMap];
}
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
if([mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MKPointAnnotation*annotation in MapViewHome.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 2; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 2; // Add a little extra space on the sides
region = [mapView regionThatFits:region];
[MapViewHome setRegion:region animated:YES];
}
-(void)loadMap{
int Coordinates = (int)[NSLat count];
CLLocationCoordinate2D coordinateArray[Coordinates];
// loop through coordinates
for (int i = 0; i < Coordinates; ++i) {
// write data from the CLLocationCoordinate2D stored in the wrapper
// to the primitive data array 'coordinateArray'
coordinateArray[i] = [NSLat[i] coordinate];
}
// then generate the routeLine.
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:Coordinates];
[MapViewHome setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
[MapViewHome addOverlay:self.routeLine];
MapViewHome.mapType = MKMapTypeHybrid;
[self zoomToFitMapAnnotations:MapViewHome];
}
so I have this code which drops a single Pin in google maps on my iPhone app.
Do you know how I would be able to drop multiple Pins to show the different locations of the business?
#import "NewClass.h"
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = 40.707184;
region.center.longitude = -73.998392;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
MapPin *ann = [[MapPin alloc] init];
ann.title = #"Brooklyn Bridge";
ann.subtitle = #"New York";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
My duplication attempt
MKCoordinateRegion region = { {0.0, 0.0}, {0.0,0.0}};
region.center.latitude = 53.0838491;
region.center.longitude = -2.7998707;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Making Waves Swim School";
ann.subtitle = #"Clutton, Cheshire";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
MKCoordinateRegion region2 = { {0.0, 0.0}, {0.0,0.0}};
region2.center.latitude = 53.199801;
region2.center.longitude = -2.89744;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.01f;
[mapview setRegion:region2 animated:YES];
NewClass *ann2 = [[NewClass alloc] init];
ann2.title = #"Chester Uni";
ann2.subtitle = #"Chester, Cheshire";
ann2.coordinate = region.center;
[mapview addAnnotation:ann2];
ann2.coordinate = region.center;
should be
ann2.coordinate = region2.center;
So that the 2 pins are not having the exact same coordinates.
I am trying to display multiple annotations on my mapView from an array. The annotation coordinates are parsed from an XML file and stored in the array as [currentCall longitude] and [currentCall latitude]. My question is, what is the syntax for "calling" the array? (I don't know if that's how you say it). In another part of my application, I display the parsed XML results in a table and use "JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];" to "call" the array. How do I do it for displaying my annotations? Everything else works fine except that little part.
Here is the implementation file:
#implementation SecondViewController
#synthesize mapView;
XMLParser *xmlParser;
-(IBAction)getlocation {
mapView.showsUserLocation = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}
-(IBAction)changeSeg:(id)sender {
if (segment.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}
if (segment.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeSatellite;
}
if (segment.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeHybrid;
}
}
-(void)viewDidLoad {
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion WCCCA = { {0.0, 0.0} , {0.0, 0.0} };
WCCCA.center.latitude = 45.53540820864449;
WCCCA.center.longitude = -122.86178648471832;
WCCCA.span.longitudeDelta = 0.02f;
WCCCA.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"WCCCA";
ann1.subtitle = #"Washington County Consolidated Communications Agency";
ann1.coordinate = WCCCA.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion CALL = { {0.0, 0.0} , {0.0, 0.0} };
CALL.center.latitude = [currentCall.latitude doubleValue];
CALL.center.longitude = [currentCall.longitude doubleValue];
CALL.span.longitudeDelta = 0.02f;
CALL.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = [currentCall currentCallType];
ann2.subtitle = [currentCall location];
ann2.coordinate = CALL.center;
[mapView addAnnotation: ann1];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorRed;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
#end
If I am understanding your code properly, you should be able to just iterate through your parsed XML (it appears that the output the from your xmlParser object is an NSArray) and add the annotations to the mapView within the loop.
You can use the C function CLLocationCoordinate2DMake() to create the coordinate data structure for your annotation.
NSArray *callsArray = [xmlParser calls];
for (JointCAD *call in callsArray) {
Annotation *ann = [[Annotation alloc] init];
ann.title = [call currentCallType];
ann.subtitle = [call location];
ann.coordinate = CLLocationCoordinate2DMake([call latitude], [call longitude]);
[mapView addAnnotation:ann];
}