I read the current location's latitude and longitude values and then pin that location in iPhone successfully. Now I want to read that place name using this latitude and longitude values.
I used the following code to read the find the current location:
- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation *whereIAm = userLocation.location;
NSLog(#"I'm at %#", whereIAm.description);
latitude = whereIAm.coordinate.latitude;
longitude = whereIAm.coordinate.longitude;
NSLog(#"LAT : %f",latitude);
NSLog(#"LONG : %f",longitude);
mapView.mapType=MKMapTypeStandard;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude =latitude; //22.569722 ;
region.center.longitude = longitude; //88.369722;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #" Welcome";
ann.subtitle = #"Hi";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation = YES;
mapView.delegate = self;
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id<MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
}
[mapView.userLocation setTitle:#"I am here"];
return pinView;
}
Use the MKReverseGeocoderDelegate Protocol like this :
#interface YourViewController : UIViewController <MKReverseGeocoderDelegate>
{
}
#implementation YourViewController
- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation *whereIAm = userLocation.location;
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:whereIAm.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
Now implement the following method delegate to get the response :
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSLog(#"I'm at %#", placemark.locality);
}
For more informations look at this : http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/
Related
I have a an array of latitude and longitude and using for loop am displaying the MKPointAnnotation on the map. I want to show a view as a popup with data when the specific MKPointAnnotation is tapped.
Here is my code -
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 10000, 10000);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
[_locationManager stopUpdatingLocation];
NSArray *set = [[NSArray alloc] init];
for (int i = 0; i < _json.count; i++) {
_name = [_json[i] valueForKey:#"name"];
_class = [_json[i] valueForKey:#"class"];
set = [_json[i] valueForKey:#"set"];
if (setFreeHour.count != 0) {
for (int j=0;j<set.count;j++) {
NSDictionary *dict = [[NSDictionary alloc] init];
dict = set[j];
_lat = dict[#"latitude"];
_longi = dict[#"longitude"];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [_lat doubleValue];
coordinate.longitude = [_longi doubleValue];
// Add an annotation
MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
point1.coordinate = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
point1.title = _name;
point1.subtitle = _class;
[self.mapView addAnnotation:point1];
}
}
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
if (!pinView) {
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"annotation"];
pinView.calloutOffset = CGPointMake(0, 0);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showDetails)];
[pinView addGestureRecognizer:tap];
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
- (void)showDetails{
self.popup.hidden = NO;
}
//PS: popup is a view that contains labels. I want to pass data from MKPointAnnotation to the view
Why don't you use mapView(_:didSelect:) method instead of UITapGestureRecognizer?
(void)mapView:(MKMapView *)mapview didSelectAnnotationView:(MKAnnotationView *)view {
// 1. get data from view(MKAnnotationView)
// 2. pass data to another view
}
https://developer.apple.com/documentation/mapkit/mkmapviewdelegate/1452393-mapview
I am using mapview and want to add custom image to show the location in map view , how to add image i am not able to add. this code i have used.
-(void)addAllPins
{
self.mapView.delegate=self;
for(int i = 0; i < name1.count; i++)
{
[self addPinWithTitle:name1[i] AndCoordinate:arrCoordinateStr[i]];
}
}
-(void)addPinWithTitle:(NSString *)title AndCoordinate:(NSString *)strCoordinate
{
MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
// clear out any white space
strCoordinate = [strCoordinate stringByReplacingOccurrencesOfString:#" " withString:#""];
// convert string into actual latitude and longitude values
NSArray *components = [strCoordinate componentsSeparatedByString:#","];
double latitude = [components[0] doubleValue];
double longitude = [components[1] doubleValue];
// setup the map pin with all data and add to map view
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
mapPin.title = title;
mapPin.coordinate = coordinate;
// UIImage *image = [UIImage imageNamed:#"hover.9.png"];
// [[self.mapView viewForAnnotation:mapPin] setImage:image];
[self.mapView addAnnotation:mapPin];
}
i want to set zoom in scale also . can some help me to solve this.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
// NSLog(#"%#", [self deviceLocation]);
//View Area
MKCoordinateRegion region = self.mapView.region;
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.015;
region.span.longitudeDelta = 0.015;
[mapView setRegion:region animated:YES];
[self addAllPins];
}
My code is help you.You can put custom pin annotation
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = #"StandardPin";
MKAnnotationView *aView = (MKAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (aView == nil)
{
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
}
aView.image = [UIImage imageNamed : #"Location_OnMap"];
aView.annotation = annotation;
aView.calloutOffset = CGPointMake(0, -5);
aView.draggable = YES;
aView.enabled = YES;
return aView;
}
You need to use mapView Delegate Method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"MKPinAnnotationView"];
if (pinView ==nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"MKPinAnnotationView"];
pinView.animatesDrop = YES;
}
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorGreen;//if you want
return pinView;
}
else
{
static NSString *viewId = #"MKAnnotationView";
MKAnnotationView *annotationView = (MKAnnotationView*)
[mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:viewId];
}
annotationView.canShowCallout=YES;
annotationView.image = [UIImage imageNamed:#"yourImage"];//set your image here
return annotationView;
}
}
2.) to zoom
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(yourLocation.coordinate, 100, 100);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
It will help.Thank you.
I'm having issues getting annotations to show up on a map in an iOS application.
I have followed the conventions in the documentation. From debugging it seems that viewForAnnotation is never being called for the annotation point.
The code for the View Controller is:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotations:[self createAnnotations]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"viewForAnnotation");
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] init];
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"foo"];
[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
return pinView;
}
- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
return annotations;
}
#end
One obvious problem is here in the createAnnotations method:
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
The coordinate property of the annotation is never set.
Set it after creating the annotation:
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coord; // <-- add this line
[annotations addObject:annotation];
He is desperately waiting for the solution... I am displaying the current user location and some custom annotations, but, when I am trying to zoom in into the map my map automatically redirecting to the current location with default zoom that is because of the span.please check my code and correct me.(i can able to zoom in other annotations when current location not displaying both simulator and device )
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
[self loadMultipleAnnotations];
self.map_view.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D coordinate;
coordinate.latitude = newLocation.coordinate.latitude;
coordinate.longitude = newLocation.coordinate.longitude;
MKCoordinateSpan span;
span.latitudeDelta = 5;
span.longitudeDelta = 5;
MKCoordinateRegion region;
region.center = coordinate;
region.span= span;
[self.map_view setRegion:region];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate;
[self.map_view setRegion:region animated:YES];
double radius = 40000.0;
MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
circle1.title = #"Current location Marking";
[self.map_view addOverlay:circle1];
}
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKCircle class]])
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:(MKCircle*)overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.0];
circleView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
circleView.lineWidth = 3;
return circleView;
}
return nil;
}
-(void)loadMultipleAnnotations
{
NSMutableArray *arrLatti = [NSMutableArray arrayWithObjects:#"27.175015",#"28.171391",#"29.169005",#"22.105999",#"17.811456",#"21.453069",#"22.593726",#"38.444985",#"35.603719",#"35.603719",#"36.844461",#"35.889050",#"33.651208",#"38.238180",#"36.862043",#"36.949892",#"37.142803",#"37.71859", nil];
NSMutableArray *arrLong = [NSMutableArray arrayWithObjects:#"78.042155",#"79.037090",#"80.043206",#"75.761719",#"79.804688",#"81.562500",#"79.277344",#"-121.871338",#"-118.641357",#"-120.536499",#"-120.591431",#"-116.334229",#"-116.971436",#"-121.827393",#"-117.784424",#"-119.564209",#"-118.289795",#"-122.299805", nil];
NSMutableArray *arrTitle = [NSMutableArray arrayWithObjects:#"relative1",#"criminal1",#"criminal2",#"criminal3",#"criminal4",#"criminal5",#"criminal6",#"criminal7",#"criminal8",#"criminal9",#"relative2",#"client",#"criminal10",#"relative3",#"criminal11",#"relative4",#"criminal13",#"Offender", nil];
NSMutableArray *arrList = [[NSMutableArray alloc]init];
for (int i= 0; i< [arrLatti count]; i++)
{
List *list = [[List alloc]init];
list.latitt = [arrLatti objectAtIndex:i];
list.log = [arrLong objectAtIndex:i];
list.title = [arrTitle objectAtIndex:i];
[arrList addObject:list];
List *obj = [arrList objectAtIndex:i];
CLLocationCoordinate2D locationCoordinate;
locationCoordinate.latitude = [obj.latitt floatValue];
locationCoordinate.longitude = [obj.log floatValue];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate =locationCoordinate;
pointAnnotation.title = obj.title;
[self.map_view addAnnotation:pointAnnotation];
MKCoordinateSpan span;
span.latitudeDelta = 10;
span.longitudeDelta = 10;
MKCoordinateRegion region;
region.center = locationCoordinate;
region.span = span;
[self.map_view selectAnnotation:pointAnnotation animated:YES];
[self.map_view setRegion:region animated:YES];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = #"reuseid";
MKAnnotationView *av = [_map_view dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] ;
}
else
{
av.annotation = annotation;
}
if ([annotation.title isEqualToString:#"criminal1"] || [annotation.title isEqualToString:#"Offender"] || [annotation.title isEqualToString:#"criminal2"]|| [annotation.title isEqualToString:#"criminal3"]|| [annotation.title isEqualToString:#"criminal4"]|| [annotation.title isEqualToString:#"criminal5"]|| [annotation.title isEqualToString:#"criminal6"]|| [annotation.title isEqualToString:#"criminal7"]|| [annotation.title isEqualToString:#"criminal8"]|| [annotation.title isEqualToString:#"criminal9"]|| [annotation.title isEqualToString:#"criminal10"]|| [annotation.title isEqualToString:#"criminal11"]|| [annotation.title isEqualToString:#"criminal12"]|| [annotation.title isEqualToString:#"criminal13"]|| [annotation.title isEqualToString:#"criminal14"]|| [annotation.title isEqualToString:#"criminal15"])
{
av.image = [UIImage imageNamed:#"marker.png"];
av.centerOffset = CGPointMake(0.0f, -16.5f);
}
else
{
av.image = [UIImage imageNamed:#"markerBlue.png"];
av.centerOffset = CGPointMake(0.0f, -24.5f);
}
av.canShowCallout = YES;
return av;
}
The trouble is this line in your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation function. Every time iOS gets a new location it will set the map view to look at it...
[self.map_view setRegion:region animated:YES];
…and add another circle without removing the previous one. Why don't you just set self.map_view.showsuserLocation = true? It won't move the map view, but it will draw a nice blue dot and a circle where ever the device is on the map.
through below code i over come my problem .. now its looks me i have solved my issue
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if ( !location )
{
CLLocationCoordinate2D locationCoordinate;
locationCoordinate.latitude = userLocation.coordinate.latitude;
locationCoordinate.longitude = userLocation.coordinate.longitude;
location = userLocation.location;
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(0.1, 0.1);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate =locationCoordinate;
[self.map_View setCenterCoordinate:locationCoordinate animated:YES];
}
}
I created a map view in my app. Now I want the user to be able to add several addresses to drop pins at those locations. I also want the user to be able to remove these pins.
Does anyone know where I can find a good tutorial, or where to start? I have no experience with mapviews...
try this......
-
(void)ShowPins
{
activity.hidden=YES;
[activity stopAnimating];
double lat;
double lng;
for (int ijk=0; ijk<arrayLocationList.count; ijk++)
{
/*Set your lat and long here*/
lat=[[[[arrayLocationList objectAtIndex:ijk]objectForKey:#"location"] objectForKey:#"lat"] doubleValue];
lng=[[[[arrayLocationList objectAtIndex:ijk]objectForKey:#"location"] objectForKey:#"lng"] doubleValue];
CLLocationCoordinate2D geos = CLLocationCoordinate2DMake(lat, lng);
MKPlacemark* marker = [[MKPlacemark alloc] initWithCoordinate:geos addressDictionary:nil];
[mapVieww addAnnotation:marker];
}
CLLocationCoordinate2D coord1 = {.latitude = lat, .longitude =lng};
MKCoordinateSpan span = {.latitudeDelta = .03,.longitudeDelta = .03};
MKCoordinateRegion region = {coord1, span};
[mapVieww setRegion:region];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
{
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation1"];
UILabel *lable=[[UILabel alloc]init];
[newAnnotation addSubview:lable];
newAnnotation.pinColor = MKPinAnnotationColorRed;
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = NO;
[newAnnotation setSelected:YES animated:YES];
return newAnnotation;
}
To Drop pins at several locations u have to try this code:
You need to put all those location latitude and longitude in an array and call this function everytime after incrementing the num value
-(void)Mapview
{ NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = [latit[num]doubleValue];
theCoordinate1.longitude = [longit[num]doubleValue];
Annotation* myAnnotation1=[[Annotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
[map_view addAnnotation:myAnnotation1];
map_view.showsUserLocation = NO;
[annotations addObject:myAnnotation1];
MKMapRect rect = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 20);
if (MKMapRectIsNull(rect)) {
rect = pointRect;
} else {
rect = MKMapRectUnion(rect, pointRect);
}
}
map_view.visibleMapRect = rect;
}
You can edit this code by adding leftcallout acessoryview or modify the contents in callout view .
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
map_view.showsUserLocation= NO;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"AnnotationIdentifier"];
if(pinView == nil)
{
pinView = pinView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:#"currentloc"] ;
pinView.centerOffset = CGPointMake(0,60);
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
}
return pinView;
}
To Remove the pins you can use this code
[self.map_view removeAnnotations:map_view.annotations];
(or)
[self.map_view removeAnnotations: [self.map_view.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"!(self isKindOfClass: %#)", [MKUserLocation class]]]];