0

LocationSource位置データで私を助けるための単なるインターフェースです。これは、Location オブジェクトをパラメーター (lat、long、およびその他の情報) として受け取る onLocationChanged(Location location)メソッドを使用します。

したがって、ここに書かれていることをよく理解している場合:

A GoogleMap object has a built-in location provider for its my-location layer, but it can be replaced with another one that implements this interface.

...GoogleMap オブジェクト用の新しい(ビルトイン ロケーション プロバイダー) ロケーション プロバイダーがありますが、 を利用LocationManagerしてそこから選択することはできます

私のコードは以下のとおりです (ご覧のとおり、私は LocationManager を使用していません)。現在地をクリックすると、非常に正確な場所が取得されます ( mMap.setMyLocationEnabled(true); ) GPS がオンになっている場合、現在地を取得できます。私はwifiしか持っていません。

私の質問は次のとおりです。この新しいプロバイダーは、GPS/ネットワークとは「異なる」ものですか? locationManager を使用しないと問題が発生しますか? (はい、公式アプリについては、LocationManagerを使用してすべてのケースを確認した方がよいことはわかっていますが、それでも機能します)。

私の推測では、GPS またはネットワークのいずれかを使用しますが、両方を有効にしている場合、どちらがデフォルトですか? アプリが最初に使用するプロバイダーはどれですか?

コード:

public class MainActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener, LocationSource, LocationListener {

private GoogleMap mMap;
private OnLocationChangedListener mapLocationListener=null;

private static final LatLng SKG_VIEW = new LatLng(40.6402778, 22.9438889);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setUpMapIfNeeded();               

//   mMap = ( (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)  ).getMap();

}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


/**  **************** SetUp Map if needed *****************
 * 
 * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
 * installed) and the map has not already been instantiated.. This will ensure that we only ever
 * call {@link #setUpMap()} once when {@link #mMap} is not null.
 * <p>
 * If it isn't installed {@link SupportMapFragment} (and
 * {@link com.google.android.gms.maps.MapView
 * MapView}) will show a prompt for the user to install/update the Google Play services APK on
 * their device.
 * <p>
 * A user can return to this Activity after following the prompt and correctly
 * installing/updating/enabling the Google Play services. Since the Activity may not have been
 * completely destroyed during this process (it is likely that it would only be stopped or
 * paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in
 * {@link #onResume()} to guarantee that it will be called.
 */
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setMyLocationEnabled(true);
        mMap.setOnMapClickListener(this);


             /***
              *         *****   CAMERA ***** 
              ***/


     // Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(SKG_VIEW)      // Sets the center of the map to SKG_VIEW point
            .zoom(7)                   // Sets the zoom
            .build();                   // Creates a CameraPosition from the builder

        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}
/** ******************   Add Markers ***************************
 * 
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * 
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    mMap.addMarker(new MarkerOptions().position(new LatLng(10, 10)).title("Marker 2"));
    mMap.addMarker(new MarkerOptions().position(new LatLng(40, 40)).title("Marker 2"));

}

/**
 * 
 * *****************  onMapClick *******************************
 * 
 */

@Override
public void onMapClick(LatLng point) {
        mMap.animateCamera(CameraUpdateFactory.newLatLng(point));

}

@Override
public void onMapLongClick(LatLng point) {
    //dont need it right now        
}

@Override
public void activate(OnLocationChangedListener listener) {
    // LocationSource
    this.mapLocationListener=listener;
}

@Override
public void deactivate() {
    // LocationSource
    this.mapLocationListener=null;
}


public void OnLocationChanged(Location location)    {                               //LocationListener
        if( mapLocationListener != null )   {
            mapLocationListener.onLocationChanged(location);

            double lat = location.getLatitude();
            double lon = location.getLongitude();

            Log.v("TAG", "i was here");
        }
}


@Override   
public void onProviderDisabled(String provider) {                                   //LocationListener
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {                                    //LocationListener
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


}
4

1 に答える 1

1

この新しいプロバイダーは、GPS/ネットワークとは「異なる」ものですか?

いいえ。プロバイダーではなくLocationSource、インターフェイスです。

locationManager を使用しないと問題が発生しますか?

必要に応じて、独自の custom を介して乱数ジェネレーターから場所を取得するLocationSourceこともできます。これはユーザーを混乱させる可能性があることに注意してください。:-)

私の推測では、GPS またはネットワークのいずれかを使用しますが、両方を有効にしている場合、どちらがデフォルトですか?

Maps V2 はクローズド ソースであるため、確実に知る方法はなく、その動作は文書化されていません。利用可能な場合はより正確な位置を使用すると思いますが、GPS プロバイダーが最初の修正を待っている間は、おそらく両方のプロバイダーを使用します。

于 2013-04-10T17:04:35.140 に答える