哪个是LocationSource的默认提供程序?

时间:2013-04-10 16:48:08

标签: android

LocationSource只是一个帮助我处理位置数据的界面。 它使用了onLocationChanged(Location location)方法,该方法将Location对象作为参数(lat,long和其他信息)。

所以,如果我完全理解这里所说的内容:

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

}


}

1 个答案:

答案 0 :(得分:1)

  

这个新的提供商与GPS /网络有什么“不同”?

没有。 LocationSource is an interface,而不是提供商。

  

如果我不使用locationManager会出现问题?

如果您愿意,欢迎您通过自己的自定义LocationSource将您的位置来自随机数生成器。请注意,这可能会使用户感到困惑。 : - )

  

我的猜测是它使用GPS或网络,但如果我同时启用哪一个是默认值?

由于Maps V2是封闭源代码,我们无法确切知道,而AFAIK的行为没有记录。我认为它将使用更准确的位置,并且它可能使用两个提供商,而GPS提供商正在等待其首次修复。