Loading current geolocation on UIWebView

≡放荡痞女 提交于 2020-01-07 04:33:31

问题


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");
}


来源:https://stackoverflow.com/questions/6978467/loading-current-geolocation-on-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!