我想从我的 iPhone 应用程序获取用户的当前位置。我想在我的应用程序中显示用户当前的位置,例如国家/地区名称、纬度、经度信息。我也想在谷歌地图上显示位置。我也尝试过谷歌搜索,但无法得到确切的答案。我已经得到了要使用的信息CLLocationManager
在我的应用程序中跟踪位置。我该如何使用这个?我已经从 Apple Documents 下载了一个示例应用程序。链接在这里:https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html
你能帮我解决这个问题吗?提前致谢。
1)我已经得到了要使用的信息CLLocationManager
在我的应用程序中跟踪位置。我该如何使用这个?
在.h文件中
#include <CoreLocation/CLLocationManagerDelegate.h>
#include <CoreLocation/CLError.h>
#include <CoreLocation/CLLocation.h>
#include <CoreLocation/CLLocationManager.h>
CLLocationManager * myLocationManager;
CLLocation * myLocation;
在 .m 文件中:-
-(void)findMyCurrentLocation
{
self.myLocationManager = [[[CLLocationManager alloc] init] autorelease];
[[self myLocationManager] setDelegate:self ];
[myLocationManager startUpdatingLocation];
double latitude=34.052234;
double longitude=-118.243685;
CLLocation *defaultLocation =[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[self setMyLocation:defaultLocation];
[defaultLocation release];
if( [CLLocationManager locationServicesEnabled] )
{
NSLog(@"Location Services Enabled....");
locationServicesEnabled=TRUE;
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information"
message:@"Fetching your current location."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ];
[alert release];
}
else
{
NSLog( @"Location Services Are Not Enabled...." );
locationServicesEnabled=FALSE;
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information"
message:@"Location service is not enable. Please enable it from settings."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ];
[alert release];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self setMyLocation:newLocation];
NSString *tempLat = [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.latitude) ];
NSString *tempLong= [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.longitude)];
appDelegate.curlat = tempLat;
appDelegate.curlong = tempLong;
}
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
{
printf("\nerror");
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Error"
message:@"Error while getting your current location."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ];
[alert release];
}
2)。我想在我的应用程序中显示用户的当前位置,例如国家/地区名称信息。
为此,您可以使用谷歌反向地理编码 OR MK反向地理编码器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)