交互设计指南 iphone_iPhone当前位置指南

交互设计指南 iphone

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit

本教程由SDKExpert.net管理员Aaron Wojnowski发布。 要查看更多iPhone教程,请访问

www.sdkexpert.net. www.sdkexpert.net

This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn how to find the user's country code and country name, along with a numerical longitude and latitude.

这是一个非常简单的教程,可轻松查找用户的当前位置。 在本教程中,您将学习如何查找用户的国家/地区代码和国家/地区名称以及数字经度和纬度。

Prior to doing this, add the CoreLocation framework. Make sure to import this framework as well.

在执行此操作之前,请添加CoreLocation框架。 确保也导入此框架。

#import <CoreLocation/CoreLocation.h>

Firstly, let's get into how to find the user's country code. Country codes can be US, CA, MX, UK, etc. The code to do this is very simple and is only 2 lines long, and it is as follows:

首先,让我们开始研究如何找到用户的国家代码。 国家/地区代码可以是US,CA,MX,UK等。执行此操作的代码非常简单,只有2行,如下所示:

NSLocale *locale = [NSLocale currentLocale];
	NSString *countryCodes = [locale objectForKey:NSLocaleCountryCode];

Firstly, we create an NSLocale named Locale and we set the locale to the currentLocale. This means that we find the user's current local location. NSLocale is also used in automated localizing for the iPhone SDK. We then create an NSString called CountryCodes. This NSString is auto-released so we have to use it quickly. If you would like to have an allocated and retained NSString, you will have to assign the retain and nonatomic properties and also synthesize the string. If we add an NSLog statement, we can find the user's country code based on the NSLocale. We can set this up as follows:

首先,我们创建一个名为Locale的NSLocale,并将语言环境设置为currentLocale。 这意味着我们找到了用户的当前本地位置。 NSLocale还用于iPhone SDK的自动本地化。 然后,我们创建一个名为CountryCodes的NSString。 此NSString是自动释放的,因此我们必须快速使用它。 如果您想要分配和保留NSString,则必须分配keep和nonatomic属性,并合成字符串。 如果添加NSLog语句,则可以基于NSLocale查找用户的国家代码。 我们可以如下设置:

NSLocale *locale = [NSLocale currentLocale];
	NSString *countryCodes = [locale objectForKey:NSLocaleCountryCode];
        NSLog(@"%@",countryCodes);

Now, to find the country name using NSLocale, we can add this line of code after the NSString *countryCodes is created:

现在,要使用NSLocale查找国家/地区名称,我们可以在创建NSString * countryCodes之后添加以下代码行:

NSString *countryName = [locale displayNameForKey:NSLocaleCountryCode value:countryCodes];

This code above selects the display name from the locale's country code that we have.

上面的这段代码从我们拥有的语言环境的国家代码中选择显示名称。

Overall, it is very easy to find the user's current location. This way is used for localization and may be a somewhat invasion of a user's privacy. Therefore, if using NSLocale, we should add a UIAlertView to ask the user if we can use their current location before finding the NSLocale. Now, we will find the user's current location via a longitude and latitude. This method is great for finding the closest city or the closest location.

总体而言,很容易找到用户的当前位置。 这种方式用于本地化,可能在某种程度上侵犯了用户的隐私。 因此,如果使用NSLocale,我们应该添加UIAlertView来询问用户是否可以在找到NSLocale之前使用其当前位置。 现在,我们将通过经度和纬度找到用户的当前位置。 此方法非常适合查找最近的城市或最近的位置。

Firstly, we can add this code in an IBAction or a custom method:

首先,我们可以将此代码添加到IBAction或自定义方法中:

	self.locationManager = [[[CLLocationManager alloc] init] autorelease];
	locationManager.delegate = self;

	[locationManager startUpdatingLocation];

After doing this, add this to your .h file:

完成此操作后,将其添加到您的.h文件中:

CLLocationManager *locationManager;

Additionally, add the properties:

此外,添加属性:

@property (nonatomic, retain) CLLocationManager *locationManager;
@synthesize locationManager;

We also want to implement the delegate protocol, so add to your .h file. Mine looked like this when finished:

我们还希望实现委托协议,因此将其添加到您的.h文件中。 我的完成后看起来像这样:

@interface RootViewController : UIViewController <CLLocationManagerDelegate>

The above code creates locationManager and initiates it. It also sets the delegate to "self" and starts updating the location. Now, we want to add a delegate method that fires when the location manager finishes updating the current location.

上面的代码创建并启动locationManager。 它还将委托设置为“ self”并开始更新位置。 现在,我们要添加一个委托方法,该委托方法将在位置管理器完成更新当前位置时触发。

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

	CLLocation *location = newLocation;
	NSLog(@"Our current Latitude is %f",location.coordinate.latitude);
	NSLog(@"Our current Longitude is %f",location.coordinate.longitude);

	float currentLatitude = location.coordinate.latitude;
	float currentLongitude = location.coordinate.longitude;

	[locationManager stopUpdatingLocation];

}

This code creates CLLocation from the location that we updated to. It then creates 2 floats and sets those to the corresponding longitude and latitude. We can also add an error method that fires if there is an error with the currentLocation:

此代码从我们更新到的位置创建CLLocation。 然后,它创建2个浮点并将其设置为相应的经度和纬度。 我们还可以添加一个error方法,该方法将在currentLocation存在错误时触发:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

	NSLog(@"There is an error updating the location");

}

You should now know how to get your current longitude and latitude and also get the current country. I hope you have enjoyed this tutorial and thanks for reading.

现在,您应该知道如何获取当前的经度和纬度以及如何获取当前的国家/地区。 希望您喜欢本教程,并感谢您的阅读。

翻译自: https://www.experts-exchange.com/articles/3901/iPhone-Current-Location-Tutorial.html

交互设计指南 iphone

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值