如何在Ti.map上的苹果地图上添加'current location button'(导航标记)

How to add 'current location button'(navigation mark) on apple map on Ti.map

我正在使用 Ti.map 我的钛金属 iOS/android

至于Android

有自动添加当前位置按钮

(我不知道这个按钮叫什么,所以我暂时叫这个'current location button'。

不过至于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' 与我调用的按钮无关 'current location button'

所以我手动制作了另一个按钮并连接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');
        }
    });

现在可以正常使用了,感谢您的帮助。

创建地图时需要额外设置属性:

require('ti.map').createView({mapType:MapModule.SATELLITE_TYPE, userLocation:true});

这应该可以解决问题;-)

/约翰