0

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

}


}
4

1 回答 1

1

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

No.LocationSource是一个接口,而不是一个提供者。

如果我不使用 locationManager 会有问题吗?

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

我的猜测是它使用 GPS 或网络,但如果我都启用了哪个是默认设置?

由于 Maps V2 是封闭源代码,我们无法确定,并且 AFAIK 的行为没有记录。我假设它将使用更准确的可用位置,并且它可能会在 GPS 提供商等待其第一次修复时同时使用这两个提供商。

于 2013-04-10T17:04:35.140 回答