for iOS開發指南(12):在iOS 中使用地理定位
在開始這篇教程之前,你應該預先閱讀并按下面的這篇教程實際操作過:
這篇教程講述了定位你的iOS 的基本步驟(使用經緯度),并使用 (反向地址編碼)來轉換成可以閱讀的地址,如下圖所示:
設計用戶界面
這個示例程序使用兩個主要的組件設計而成:一個(在左邊)和一個。
在中,設置Align屬性為來保留UI的整個左邊為它所用。然后在上創建下面這些子控件:
一個顯示標題為“ Demo”的組件
一個開關控件來選擇開啟/關閉
在你創建完這些組件之后,選擇所有的項目,然后在屬性中選擇為l。這允許來同時顯示一個Label和詳細信息文本。
位置傳感器
位置傳感器由組件封裝。
觸發一個事件,當檢測到移動時。你可以使用屬性來調整的靈敏度。如果你設置為“10”,當你移動了“10”米時,就會觸發事件。
從組件獲取位置信息(經緯度)
首先,組件需要激活才能使用。可以基于你的輸入來開啟/關閉,例如使用一個組件,或其他應用程序事件。
這里有一段代碼,它根據組件值的更改來控制:
procedure TForm44.Switch1Switch(Sender: TObject);
begin
LocationSensor1.Active := Switch1.IsChecked;
end;
就像之前提到過的,當你移動iOS 時就會觸發一個事件。你可以在這個事件處理過程中使用它的參數來顯示當前的位置,方法如下:
procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;
const OldLocation, NewLocation: TLocationCoord2D);
begin

// Show current location
ListBoxItemLatitude.ItemData.Detail := NewLocation.Latitude.ToString;
ListBoxItemLongitude.ItemData.Detail := NewLocation.Longitude.ToString;
end;
通過組件使用 Maps來顯示當前的位置
之前在“iOS教程:在iOS應用程序中使用組件”這篇教程中講到過的,組件封裝了iOS的。
你可以從組件來調用 Maps,使用如下URL參數:
(-value),(- value)&=embed
因此你可以添加這個URL到你之前創建的事件處理過程中,如下:
procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;
const OldLocation, NewLocation: TLocationCoord2D);
var
URLString: String;
begin
// code for previous step goes here
// Show Map using Google Maps
URLString := Format(

'https://maps.google.com/maps?q=%s,%s&output=embed',
[NewLocation.Latitude.ToString, NewLocation.Longitude.ToString]);
WebBrowser1.Navigate(URLString);
end;
使用反向地理編碼
是一個封裝了地理編碼(或反向地理編碼)服務的對象。
地理編碼是翻譯地理數據的過程,例如地址和郵編,轉換成地理坐標。反向地理編碼是將地地理坐標轉換成地理數據的過程,例如地址:
在我們這個例子中,我們使用將我們的位置(以經緯度的形式)“反向地理編碼”成可讀的地址信息。
這里是使用的基本操作步驟:
1. 創建一個的實例。
2.定義一個事件,以便你之后能夠接收到這個事件。
3.設置數據來執行“反向地址編碼”。
4.訪問網絡上的服務來處理地址信息。
5. 觸發一個事件。
6.你的iOS應用通過事件的參數來接收地址信息,然后更新用戶界面。
因為不是一個組件(它只是一個類),你需要通過你的代碼來定義這些步驟(你不能拖放一個組件ios 地理位置反編碼,也不能通過 來賦一個事件處理過程)。
首先,在窗體的聲明區域定義一個新的成員“:”。你也可以照著下面這段代碼來定義一個“nt過程”。
type
TForm44 = class(TForm)
// IDE defines visible (or non-visual) components here automatically
private
{ Private declarations }

FGeocoder: TGeocoder;
procedure OnGeocodeReverseEvent(const Address: TCivicAddress);
public
{ Public declarations }
end;
當你定義了這2行代碼,將光標定位到t,然后按Ctrl+Shift+C,這會在你的代碼中創建如下過程(之后你會使用到的):
procedure TForm44.OnGeocodeReverseEvent(const Address: TCivicAddress);
begin
end;
現在你可以創建一個的實例,并使用下列代碼來初始數據。
.提供了實際實現服務的類類型,“..”調用指定類類型的構造方法,然后保存到成員。你也需要指定一個事件處理過程,它在完成反向地理編碼時觸發。將t(你之前那一步剛剛定義的)賦給.。
最后ios 地理位置反編碼,如果你成功創建了一個的實例,并且沒有運行,使用地址信息調用.。當接收到數據之后,nt事件就觸發了。
procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;
const OldLocation, NewLocation: TLocationCoord2D);

begin
// code for previous steps goes here
// Setup an instance of TGeocoder
if not Assigned(FGeocoder) then
begin
if Assigned(TGeocoder.Current) then
FGeocoder := TGeocoder.Current.Create;
if Assigned(FGeocoder) then
FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;
end;
// Translate location to address
if Assigned(FGeocoder) and not FGeocoder.Geocoding then
FGeocoder.GeocodeReverse(NewLocation);
end;

在組件中顯示一個可讀的地址
之前提到過的,在反向地理編碼完成之后,一個t會觸發。
接下來,將地址參數中的屬性賦給在中的字段:
procedure TForm44.OnGeocodeReverseEvent(const Address: TCivicAddress);
begin
ListBoxItemAdminArea.ItemData.Detail := Address.AdminArea;
ListBoxItemCountryCode.ItemData.Detail := Address.CountryCode;
ListBoxItemCountryName.ItemData.Detail := Address.CountryName;
ListBoxItemFeatureName.ItemData.Detail := Address.FeatureName;
ListBoxItemLocality.ItemData.Detail := Address.Locality;
ListBoxItemPostalCode.ItemData.Detail := Address.PostalCode;
ListBoxItemSubAdminArea.ItemData.Detail := Address.SubAdminArea;
ListBoxItemSubLocality.ItemData.Detail := Address.SubLocality;
ListBoxItemSubThoroughfare.ItemData.Detail := Address.SubThoroughfare;
ListBoxItemThoroughfare.ItemData.Detail := Address.Thoroughfare;
end;
翻譯的不好,請大家賤諒!
歡迎加入 For FMX 技術支持QQ群