How to set longitude and latitude using textfield - ios

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

Related

Drop Multiple Pins on Maps, Xcode

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.

Need to showing screen that show both CLLocation pins

I have mapView that suppose to contain both user location pin and object pin. What i want, is show screen that contain both pins, no matter how large distance is between them (it suppose to be not too much). For some reason i can't show user location, screen moves to some point in the ocean. There is my code and attempts to get it:
-(MKCoordinateRegion)regionForAnnotations:(NSArray*)annotations{
MKCoordinateRegion region;
if ([annotations count] ==0){
region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000);
} else if ([annotations count] ==1 ){
id <MKAnnotation> annotation = [annotations lastObject];
region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);
} else {
CLLocationCoordinate2D topLeftCord;
topLeftCord.latitude = -90;
topLeftCord.longitude = 180;
CLLocationCoordinate2D bottomRightCord;
bottomRightCord.latitude = 90;
bottomRightCord.longitude = -180;
for (id <MKAnnotation> annotation in annotations){
topLeftCord.latitude = fmax(topLeftCord.latitude, annotation.coordinate.latitude);
topLeftCord.longitude = fmin(topLeftCord.longitude, annotation.coordinate.longitude);
bottomRightCord.latitude = fmin(bottomRightCord.latitude, annotation.coordinate.latitude);
bottomRightCord.longitude = fmax(bottomRightCord.longitude, annotation.coordinate.longitude);
}
const double extraSpace = 1.1;
region.center.longitude = topLeftCord.longitude - (topLeftCord.longitude - bottomRightCord.longitude)/2.0;
region.center.latitude = topLeftCord.latitude - (topLeftCord.latitude - bottomRightCord.latitude)/2.0;
region.span.latitudeDelta = fabs(topLeftCord.latitude - bottomRightCord.latitude)*extraSpace;
region.span.longitudeDelta = fabs(topLeftCord.longitude - bottomRightCord.longitude)*extraSpace;
}
return [self.mapView regionThatFits:region];
}
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//2
static NSString *identifier = #"Location";
MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil){
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
}
//3
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.pinColor = MKPinAnnotationColorPurple;
//4
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getRoute:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
};
-(void)setEntityCoordiate{
CLLocationCoordinate2D location;
location.latitude = [self.latitudeString doubleValue];
location.longitude = [self.longitudeString doubleValue];
CLLocationCoordinate2D user;
user.latitude = self.mapView.userLocation.coordinate.latitude;
user.latitude = self.mapView.userLocation.coordinate.longitude;
CLLocation *userLocation = [[CLLocation alloc]initWithLatitude:user.latitude longitude:user.longitude ];
CLLocation *entityLocation = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];
MKCoordinateSpan span;
span.latitudeDelta = 100;
span.longitudeDelta = 100;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 400, 400);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = location;
annotation.title = _titleForEntity;
[_locations addObject:userLocation];
[_locations addObject:entityLocation];
[_mapView addAnnotation:annotation];
}
-(void)getRoute:(UIButton*)button{
MKCoordinateRegion region = [self regionForAnnotations:_locations];
[self.mapView setRegion:region animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setEntityCoordiate];
}
There is might be "too much" code, but i can't figure out where i made mistake i paste it all. I think i might have problem with array containing location objects.
How to finally make screen with that two locations? Any advice would be appreciated, thanks!
Try creating an area flyTo that will "contain" your given annotations:
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapview.visibleMapRect = flyTo;

Mapkit adding new annotation and remove the old

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.

MapKit help. Can't get my location to show up first

I want to develop a map that shows my location and has pins of other locations on the map.
I've gotten the map to show up and the pins to show up too, it also locates me but it doesn't zoom into my location first. How can I get it to show my location first? When I open up the mapview it just shows the last pin I put into the code.
Here is my code.
mapview.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface mapview : UIViewController {
MKMapView *mapview1;
}
#property (nonatomic, retain) IBOutletMKMapView *mapview1;
-(IBAction)setMap:(id)sender;
-(IBAction)getlocation;
#end
mapview.m
#import "mapview.h"
#import "NewClass.h"
#implementation mapview
#synthesize mapview1;
-(IBAction)getlocation {
mapview1.showsUserLocation = YES;
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview1.mapType = MKMapTypeStandard;
break;
case 1:
mapview1.mapType = MKMapTypeSatellite;
break;
case 2:
mapview1.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(void)viewDidLoad {
[super viewDidLoad];
[mapview1 setMapType:MKMapTypeStandard];
[mapview1 setZoomEnabled:YES];
[mapview1 setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 39.956907;
region.center.longitude = -75.610229;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"Vigil Location";
ann.subtitle = #"Planned Parenthood of Chester County";
ann.coordinate = region.center;
[mapview1 addAnnotation:ann];
MKCoordinateRegion region1 = { {0.0, 0.0 }, {0.0, 0.0 } };
region1.center.latitude = 40.042819;
region1.center.longitude = -75.373776;
region1.span.longitudeDelta = 0.01f;
region1.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region1 animated:YES];
NewClass *ann1 = [[NewClass alloc] init];
ann1.title = #"Vigil Location";
ann1.subtitle = #"Planned Parenthood of Bryn Mawr";
ann1.coordinate = region1.center;
[mapview1 addAnnotation:ann1];
MKCoordinateRegion region2 = { {0.0, 0.0 }, {0.0, 0.0 } };
region2.center.latitude = 40.18295;
region2.center.longitude = -75.450163;
region2.span.longitudeDelta = 0.01f;
region2.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region2 animated:YES];
NewClass *ann2 = [[NewClass alloc] init];
ann2.title = #"Vigil Location";
ann2.subtitle = #"Planned Parenthood Collegeville";
ann2.coordinate = region2.center;
[mapview1 addAnnotation:ann2];
MKCoordinateRegion region3 = { {0.0, 0.0 }, {0.0, 0.0 } };
region3.center.latitude = 40.120772;
region3.center.longitude = -75.118181;
region3.span.longitudeDelta = 0.01f;
region3.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region3 animated:YES];
NewClass *ann3 = [[NewClass alloc] init];
ann3.title = #"Vigil Location";
ann3.subtitle = #"Abington Memorial Hospital";
ann3.coordinate = region3.center;
[mapview1 addAnnotation:ann3];
MKCoordinateRegion region4 = { {0.0, 0.0 }, {0.0, 0.0 } };
region4.center.latitude = 39.95361;
region4.center.longitude = -75.15267;
region4.span.longitudeDelta = 0.01f;
region4.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region4 animated:YES];
NewClass *ann4 = [[NewClass alloc] init];
ann4.title = #"Vigil Location";
ann4.subtitle = #"Philadelphia Women's Center";
ann4.coordinate = region4.center;
[mapview1 addAnnotation:ann4];
MKCoordinateRegion region5 = { {0.0, 0.0 }, {0.0, 0.0 } };
region5.center.latitude = 39.910566;
region5.center.longitude = -75.014111;
region5.span.longitudeDelta = 0.01f;
region5.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region5 animated:YES];
NewClass *ann5 = [[NewClass alloc] init];
ann5.title = #"Vigil Location";
ann5.subtitle = #"Cherry Hill Women's Center";
ann5.coordinate = region5.center;
[mapview1 addAnnotation:ann5];
MKCoordinateRegion region6 = { {0.0, 0.0 }, {0.0, 0.0 } };
region6.center.latitude = 39.74285;
region6.center.longitude = -75.550838;
region6.span.longitudeDelta = 0.01f;
region6.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region6 animated:YES];
NewClass *ann6 = [[NewClass alloc] init];
ann6.title = #"Vigil Location";
ann6.subtitle = #"Planned Parenthood of Wilmington";
ann6.coordinate = region6.center;
[mapview1 addAnnotation:ann6];
MKCoordinateRegion region7 = { {0.0, 0.0 }, {0.0, 0.0 } };
region7.center.latitude = 40.03754;
region7.center.longitude = -76.300828;
region7.span.longitudeDelta = 0.01f;
region7.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region7 animated:YES];
NewClass *ann7 = [[NewClass alloc] init];
ann7.title = #"Vigil Location";
ann7.subtitle = #"Planned Parenthood of Lancaster";
ann7.coordinate = region7.center;
[mapview1 addAnnotation:ann7];
MKCoordinateRegion region8 = { {0.0, 0.0 }, {0.0, 0.0 } };
region8.center.latitude = 40.333819;
region8.center.longitude = -75.93019;
region8.span.longitudeDelta = 0.01f;
region8.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region8 animated:YES];
NewClass *ann8 = [[NewClass alloc] init];
ann8.title = #"Vigil Location";
ann8.subtitle = #"Planned Parenthood of Reading";
ann8.coordinate = region8.center;
[mapview1 addAnnotation:ann8];
MKCoordinateRegion region9 = { {0.0, 0.0 }, {0.0, 0.0 } };
region9.center.latitude = 40.67246;
region9.center.longitude = -75.375397;
region9.span.longitudeDelta = 0.01f;
region9.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region9 animated:YES];
NewClass *ann9 = [[NewClass alloc] init];
ann9.title = #"Vigil Location";
ann9.subtitle = #"Planned Parenthood of Allentown";
ann9.coordinate = region9.center;
[mapview1 addAnnotation:ann9];
MKCoordinateRegion region10 = { {0.0, 0.0 }, {0.0, 0.0 } };
region10.center.latitude = 39.952384;
region10.center.longitude = -76.7253;
region10.span.longitudeDelta = 0.01f;
region10.span.latitudeDelta = 0.01f;
[mapview1 setRegion:region10 animated:YES];
NewClass *ann10 = [[NewClass alloc] init];
ann10.title = #"Vigil Location";
ann10.subtitle = #"Planned Parenthood of York";
ann10.coordinate = region10.center;
[mapview1 addAnnotation:ann10];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
You've set showsUserLocation but if you read the Apple docs you'll see that that function only turns on the blue dot, it doesn't zoom or pan the view. "http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/showsUserLocation
What you want to do is set the user tracking mode to userfollows.
Other code tips:
Put all those places in an array and loop over them, don't create them one at a time like that
Don't setRegion on the map for every region you make. You're trying to get the map to zoom in on the user and each setRegion changes where the map is looking. Only the last one will have any effect anyway.
You don't need to create a region just to get the coordinates for an annotation

Two Annotation Pins at a time in MKMapView

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

Resources