2016-08-23 21 views

答えて

0

また、onLocationChangedクラスを実装する必要がありますか?

ロケーションのアップデートを受信したくない場合、答えはいいえです。

これを行う最も簡単な方法はどれですか?

最も簡単な方法は、hereのように最後の既知の場所を取得することです。

0

Googleマップの例のプロジェクトgithubには、これを行う方法の例があります。

さらに、Androidスタジオには、プロジェクトの作成時に選択できるGoogleマップアクティビティテンプレートがあります。このプロジェクトにはgetLastKnownLocation()が実装されています。詳細はhereを参照してください。

1

さまざまな方法で実装することができます。私はあなたの仕事のために最善のことは自分の位置ボタンを押した後にトーストを見せることだと思う傾向があります。

public class MapActivity extends FragmentActivity implements OnMapReadyCallback { 
    private GoogleMap mMap; 

    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     //Show my location button 
     mMap.setMyLocationEnabled(true); 

     mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { 
      @Override 
      public boolean onMyLocationButtonClick() { 
       //Do something with your location. You can use mMap.getMyLocation(); 
       //anywhere in this class to get user location 
       Toast.makeText(MapActivity.this, String.format("%f : %f", 
         mMap.getMyLocation().getLatitude(), mMap.getMyLocation().getLongitude()), 
         Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     }); 
    } 
} 

また、変更するたびにユーザーの場所でトーストを表示できます。これを行うには、OnMyLocationChangeListenerを設定してください。

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 
     @Override 
     public void onMyLocationChange(android.location.Location location) { 
      //... 
     } 
    }); 
関連する問題