Function setUserLocation

  • 自位置マーカーの表示と位置情報の更新を行う — 詳細な使用例

    UserLocation オブジェクトで自位置マーカーのアイコン・スタイルを設定し、 UserLocationData で実際の緯度経度・精度・向きを更新します。 GPS連動させる場合は setUserLocationDataInterval で更新間隔も設定してください。

    メソッド 用途
    setUserLocation(ul) 自位置マーカーのスタイル設定(アイコン等)
    setUserLocationData(data) 緯度経度・精度・向きを更新
    getUserLocationData() 現在の自位置情報を取得
    setUserLocationDataInterval(sec) 位置情報の自動更新間隔を設定(秒)

    Returns void

    Warning

    • setUserLocation はマーカーのスタイル定義です。位置の更新は setUserLocationData で行います。
    • GPS連動を行う場合は Geolocation API (navigator.geolocation.watchPosition) と組み合わせてください。
    • トラッキングモード(地図の自動追従)は setTrackingMode で設定します。

    Example: Geolocation API と連携して自位置を表示する

    import * as GIA from '@ntj/gaia';

    const userLocation = new GIA.object.UserLocation({
    info: new GIA.value.GLMarkerIconInfo({
    icon: USER_LOCATION_ICON,
    size: new GIA.value.Size(30, 30),
    gravity: 'center',
    }),
    });

    // マーカースタイルを設定
    map.setUserLocation(userLocation);
    map.setTrackingMode('follow');

    // GPS位置を取得して更新
    navigator.geolocation.watchPosition((pos) => {
    const latLng = new GIA.value.LatLng(pos.coords.latitude, pos.coords.longitude);
    const heading = pos.coords.heading ?? 0;
    map.setUserLocationData(new GIA.value.UserLocationData(latLng, heading), true);
    });

    Example: デモデータ再生時の更新間隔を設定する

    import * as GIA from '@ntj/gaia';

    const intervalMs = 1000;
    map.setUserLocationDataInterval(intervalMs * 0.001); // 秒単位

    const point = new GIA.value.UserLocationData(
    new GIA.value.LatLng(35.6812, 139.7671),
    90,
    );
    map.setUserLocationData(point, true);

    Example: 自位置マーカーの投影モードを切り替える

    import * as GIA from '@ntj/gaia';

    const userLocation = new GIA.object.UserLocation({
    info: new GIA.value.GLMarkerIconInfo({ icon: USER_LOCATION_ICON }),
    });

    map.setUserLocation(userLocation);
    userLocation.setProjectionMode('perspective');

    See