インタラクティブデザインガイド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 

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 

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つの浮動小数点を作成し、対応する経度と緯度に設定します.また、currentLocationにエラーが発生したときにトリガーされるerrorメソッドを追加することもできます.
- (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