Showing posts with label longitude. Show all posts
Showing posts with label longitude. Show all posts

Tuesday, 5 August 2014

Show Coordinate on Map View

CLLocationCoordinate2D _coordinate = CLLocationCoordinate2DMake(37.3104, -122.0108);

    MKCoordinateRegion extentsRegion = MKCoordinateRegionMakeWithDistance(_coordinate, 50, 50);
    
    
    extentsRegion.span.latitudeDelta = 0.02f;
    
    extentsRegion.span.longitudeDelta = 0.02f;
    
    extentsRegion.center = _coordinate;
    
    [self.myMapView setRegion:extentsRegion animated:YES];
    
    
    //set annotation
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
    
    NSString *sttt = [[NSString alloc]initWithFormat:@"%f,%f",_coordinate.latitude,_coordinate.longitude];
    
    
    
    
    annotation.title = [NSString stringWithFormat:@"%@",address];
    annotation.subtitle = sttt;
    
    annotation.coordinate = _coordinate;
    

    [self.myMapView addAnnotation:annotation];

Get Coordinate from Address String Using CLGeocoder and Show on Map with Default Annotation

CLLocationCoordinate2D cordinate = [self geoCodeUsingAddress:@"
6138 Bollinger Road, San Jose, United States"];
    

NSLog(@"coordinate from address::%f , %f",cordinate.longitude,cordinate.latitude);






- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{

        CLGeocoder *geocoder11 = [[CLGeocoder alloc] init];
    [geocoder11 geocodeAddressString: address completionHandler:^(NSArray* placemarks, NSError* error){
        for (CLPlacemark* aPlacemark in placemarks)
        {
            // Process the placemark.
            NSString *latDest1 = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
            NSString *lngDest1 = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];
           
           
           
           NSLog(@"coordinate from address::%@ , %@",latDest1,lngDest1);
           
           
            CLLocationCoordinate2D center;
            center.latitude = aPlacemark.location.coordinate.latitude;
            center.longitude = aPlacemark.location.coordinate.longitude;
            
            

            
            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 50, 50);
            
            region.span.latitudeDelta = 0.02f;
            
            region.span.longitudeDelta = 0.02f;
            
            region.center = center;
            
            
            [self.myMapView setRegion:region animated:YES];
            
            
            
            
            
            //set annotation
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
            
            NSString *sttt = [[NSString alloc]initWithFormat:@"%f,%f",center.latitude,center.longitude];
            
            annotation.titleaddress;
            annotation.subtitle = sttt;
            
            annotation.coordinate = center;
            
            [self.myMapView addAnnotation:annotation];

           
           return center;
           
           
        }
    }];


}

Get Coordinate from Address String Using Google Api and Show on Map with Default Annotation

CLLocationCoordinate2D cordinate = [self geoCodeUsingAddress:@"Your Address"];
    

   
NSLog(@"coordinate from address::%f , %f",cordinate.longitude,cordinate.latitude);








- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    double latitude = 0, longitude = 0;
    
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    
    
    
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    
    
    
    
    if (result)
    {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    
    
    
    
    
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 50, 50);
    
    region.span.latitudeDelta = 0.02f;
    
    region.span.longitudeDelta = 0.02f;
    
    region.center = center;
    
    
    [self.myMapView setRegion:region animated:YES];
    
    
    
    
    
    //set annotation
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
    
    NSString *sttt = [[NSString alloc]initWithFormat:@"%f,%f",center.latitude,center.longitude];
    
    annotation.title = address;
    annotation.subtitle = sttt;
    
    annotation.coordinate = center;
    
    [self.myMapView addAnnotation:annotation];


    
    
    
    
    return center;
}




You will get the result like this
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Coimbatore",
               "short_name" : "Coimbatore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Coimbatore",
               "short_name" : "Coimbatore",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tamil Nadu",
               "short_name" : "TN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Coimbatore, Tamil Nadu, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 11.14356120,
                  "lng" : 77.1133590
               },
               "southwest" : {
                  "lat" : 10.89792370,
                  "lng" : 76.8830110
               }
            },
            "location" : {
               "lat" : 11.01684450,
               "lng" : 76.95583209999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 11.09502970,
                  "lng" : 77.05123890
               },
               "southwest" : {
                  "lat" : 10.89792370,
                  "lng" : 76.8830110
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}




Thursday, 22 May 2014

Get latitude and longitude of current location

Add core location framework and import them.

Now in .h file add delegate CLLocationManagerDelegate.

CLLocationManager *locationManager;
    double current_lati;

    double current_longi;


In .m class

-(void)getCurrentLatLon
{
    
    // locationManager update as location

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    current_lati = location.coordinate.latitude;
    current_longi = location.coordinate.longitude;
    
    NSLog(@"dLongitude : %f", current_longi);
    NSLog(@"dLatitude : %f", current_lati);
    
    
}