-1

我想从我的 iPhone 应用程序中获取用户的当前位置。我也尝试过谷歌搜索,但无法得到确切的答案。我正在尝试开发代码。位置将无法正确显示。我可以找到最近的位置。但我无法找到确切的位置。请给我任何想法,如何显示确切的位置。提前致谢。

MapviewAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

if([CLLocationManager locationServicesEnabled])

 {

self.locationManager.delegate=self;

self.locationManager.distanceFilter=1;

 [self.locationManager startUpdatingLocation];

}


}


-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

double miles=12.0;

double scalingFactor= ABS( cos(2 * M_PI * newLocation.coordinate.latitude /360.0) );

MKCoordinateSpan span;

span.latitudeDelta=miles/69.0;

span.longitudeDelta=miles/(scalingFactor*69.0);

 MKCoordinateRegion region;

region.span=span;

region.center=newLocation.coordinate;

[self.viewController.mapView setRegion:region animated:YES];

self.viewController.mapView.showsUserLocation=YES;

}

地图视图控制器.m

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.mapView.showsUserLocation = YES;

MKCoordinateRegion region;

 region.center = self.mapView.userLocation.coordinate;

 MKCoordinateSpan span;

span.latitudeDelta  = 20;

span.longitudeDelta = 20;

 region.span = span;

[self.mapView setRegion:region animated:YES];

}

// create a region that fill fit all the locations in it

+ (MKCoordinateRegion) getRegionThatFitsLocations:(NSArray *)locations {

// initialize to minimums, maximums

 CLLocationDegrees minLatitude = 90;

CLLocationDegrees maxLatitude = -90;

CLLocationDegrees minLongitude = 180;

 CLLocationDegrees maxLongitude = -180;


// establish the min and max latitude and longitude

 // of all the locations in the array

 for (CLLocation *location in locations) 
{


if (location.coordinate.latitude < minLatitude) 

{

 minLatitude = location.coordinate.latitude;

 }

if (location.coordinate.latitude > maxLatitude)

{

maxLatitude = location.coordinate.latitude;

}

if (location.coordinate.longitude < minLongitude) 

{

 minLongitude = location.coordinate.longitude;

}

if (location.coordinate.longitude > maxLongitude) 

{

maxLongitude = location.coordinate.longitude;

}

}



MKCoordinateSpan span;

 CLLocationCoordinate2D center;

if ([locations count] > 1) 

{

// for more than one location, the span is the diff between

// min and max latitude and longitude

 span =  MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude);

 // and the center is the min + the span (width) / 2

 center.latitude = minLatitude + span.latitudeDelta / 2;

center.longitude = minLongitude + span.longitudeDelta / 2;

} 

else 

{

// for a single location make a fixed size span (pretty close in zoom)

span =  MKCoordinateSpanMake(0.01, 0.01);

// and the center equal to the coords of the single point

// which will be the coords of the min (or max) coords

 center.latitude = minLatitude;

 center.longitude = minLongitude;

 }


 // create a region from the center and span

return MKCoordinateRegionMake(center, span);

}
4

2 回答 2

1

为什么不在userLocationVisibleMKMapView 上启用?然后用户当前位置将在地图上显示为一个蓝点。

于 2013-07-11T05:29:28.390 回答
0

基本上,每当您添加 mapView 时,您都应该在检查器中启用 showuserlocation 选项。然后,当您在设备上运行该应用程序时,该应用程序将从您的位置开始。

于 2013-07-11T09:40:13.187 回答