Loading current geolocation on UIWebView

2019-09-04 15:26发布

问题:

Is there a way where I can get the current geographic location of the user (In my case, Bengluru, India), so that I can call Google Static Map API and load the static map image on the UIWebView of the ViewController.xib

For example,
http://maps.googleapis.com/maps/api/staticmap?center=-OBTAINED_LOCATION-&zoom=14&size=512x512&maptype=roadmap&sensor=false

Here OBTAINED_LOCATION should be the one that indicates current geographic location of the user.

回答1:

User Following Code:

First access following delegates to your interface:

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface StoresMapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}

Then In .m use Following

Edit:

-(void)setMapCenter:(CLLocationCoordinate2D)location
{
NSLog(@"Current Location : %f, %f",location.latitude,location.longitude);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
//location.latitude=(float *)(self.appDelegate.currentLocationLatitude);
//location.latitude=self.appDelegate.currentLocationLongitude;
region.center=location;
[self._mapView setRegion:region animated:TRUE];
[self._mapView regionThatFits:region];
}


#pragma mark -
#pragma mark Location Manager functions

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{   
NSLog(@"Inside Location Delegate = %@", newLocation.coordinate);    

[self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];
    [self setMapCenter:newLocation.coordinate];//Call this Metod defined above

[self.locationManager stopUpdatingLocation];//Don't use this line if u want continous updation in user's location
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"ERROR");
}