我使用Ti.map我的鈦的iOS/Android的如何添加「當前位置按鈕」(航標)在蘋果地圖上Ti.map
至於Android的
有被自動添加當前位置按鈕
(我不知道這是什麼按鈕被調用,所以我稱之爲「當前位置按鈕」現在是這樣。
但是對於iOS,
當前位置按鈕不會自動添加。
有沒有辦法在蘋果地圖上添加此按鈕?
這些是我的代碼。
var mapView = Map.createView({
mapType:Map.NORMAL_TYPE,
userLocation:true,
region: {latitude:35.699058, longitude:139.326099,
latitudeDelta:0.01, longitudeDelta:0.01},
animate:false,
regionFit:true,
userLocation:true,
});
我讀this文章和發現。
- To use the `userLocation` property in iOS 8 and later, add either the
[`NSLocationWhenInUseUsageDescription`](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26)
or
[`NSLocationAlwaysUsageDescription`](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW18)
key to the iOS plist section of the project's `tiapp.xml` file.
<ti:app>
<ios>
<plist>
<dict>
<key>NSLocationAlwaysUsageDescription</key>
<string>
Specify the reason for accessing the user's location information.
This appears in the alert dialog when asking the user for permission to
access their location.
</string>
</dict>
</plist>
</ios>
</ti:app>
所以我在我的tiapp.xml中添加了這個,並且提示警告顯示正確。
但是,仍然沒有出現當前位置按鈕。
在iOS的默認蘋果地圖應用程序中,左下方有當前位置按鈕(箭頭標記)。
感謝評論。
我的第一個想法是錯誤的。 'userLocation:true'與我稱之爲'當前位置按鈕'的按鈕沒有關係
所以我手動創建了另一個按鈕並連接Ti.Geolocation。
var locationButton = Ti.UI.createButton({
backgroundImage:'/images/check.png',
right:'15dp',width:'32dp',height:'32dp'
});
mapView.add(locationButton);
locationButton.addEventListener('click',function(){
Ti.API.info("get current location");
if (Ti.Geolocation.locationServicesEnabled) {
Titanium.Geolocation.getCurrentPosition(function(e) {
if (e.error) {
Ti.API.error('Error: ' + e.error);
} else {
Ti.API.info(e.coords);
mapView.setLocation({
latitude:e.coords.latitude, longitude:e.coords.longitude, animate:false,
latitudeDelta:0.01, longitudeDelta:0.01
});
}
});
} else {
alert('Please enable location services');
}
});
現在它工作得很好,感謝您的幫助。
不同的按鈕是你所尋求的一個。 Android上的一個原生Google地圖。所以約翰的回答實際上是正確的。 –
啊,我看到這個按鈕原生只有android,對不對?所以我必須自己製作另一個按鈕才能跳到當前的用戶位置? – whitebear