2013年4月30日火曜日

Titaniumでアプリ開発(9) 現在地を表示する


GPSから現在地情報を取得するにはgetCurrentPositionメソッドを用います

var win = Ti.UI.createWindow({
title: 'Home',
backgroundColor: '#F4F4F4'
});

var view = Ti.UI.createView();

// 最初に中心となる位置をセットしておく
var mapview = Ti.Map.createView({
mapType: Ti.Map.STANDARD_TYPE,
region: {latitude:35.6911, longitude:139.7067, latitudeDelta:0.01, longitudeDelta:0.01},
animate: true,
regionFit: true,
width: 300,
height: 400
});

view.add(mapview);
win.add(view);

Titanium.Geolocation.purpose = 'サンプル';

Titanium.Geolocation.getCurrentPosition(
function(e) {
if(!e.success || e.error){
alert('位置情報が取得できませんでした');
return;
}
// 現在地をセット
latitude = e.coords.latitude;
             longitude = e.coords.longitude;
// 現在地を動的に表示する
var currentPos = Titanium.Map.createAnnotation({
latitude: latitude, 
longitude: longitude, 
pincolor: Titanium.Map.ANNOTATION_RED,
animate: true
});
    mapview.addAnnotation(currentPos);
           mapview.show(); // 隠していた地図を表示する
            mapview.setLocation({   // 現在地まで地図をスクロールする
            latitude:latitude,
            longitude:longitude,
            latitudeDelta:0.01,
            longitudeDelta:0.01
            });
}
);

win.open();

0 件のコメント:

コメントを投稿