Android Maps Api V2中是否有类似onFirstFix方法的功能?

Android Maps Api V2中是否有类似onFirstFix方法的功能?,android,google-maps,google-maps-android-api-2,Android,Google Maps,Google Maps Android Api 2,我可以通过在新的Maps Api中启用myLocation视图 Api的第一个版本有一个方法,使我能够在找到用户位置后将地图动画化到该位置。我在Api版本2中找不到这样的侦听器或位置 一旦找到用户,是否有执行操作的解决方案 谷歌地图Android API V2有一个LocationSource。从文档中: GoogleMap对象的“我的位置”层有一个内置的位置提供程序,但可以用另一个实现此接口的提供程序替换 我想你需要把它和 更新 我明白了。如果要执行类似于runOnFirstFix的操作,下

我可以通过在新的Maps Api中启用myLocation视图

Api的第一个版本有一个方法,使我能够在找到用户位置后将地图动画化到该位置。我在Api版本2中找不到这样的侦听器或位置


一旦找到用户,是否有执行操作的解决方案

谷歌地图Android API V2有一个LocationSource。从文档中:

GoogleMap对象的“我的位置”层有一个内置的位置提供程序,但可以用另一个实现此接口的提供程序替换

我想你需要把它和

更新

我明白了。如果要执行类似于runOnFirstFix的操作,下面是一个基本示例,它将等待用户的位置可用,然后设置地图动画,使其位于其位置的中心

public class MyLocationMapFragmentActivity extends FragmentActivity implements LocationListener, LocationSource
{
/**
 * Note that this may be null if the Google Play services APK is not available.
 */
private GoogleMap mMap;

private OnLocationChangedListener mListener;
private LocationManager locationManager;

private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.basic_map);

    this.mContext = this;

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //You may want to pass a different provider in as the first arg here
    //depending on the location accuracy that you desire
    //see LocationManager.getBestProvider()
    Criteria locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    locationManager.requestLocationUpdates(locationManager.getBestProvider(locationCriteria, true), 1L, 2F, this);

    setUpMapIfNeeded();
}

@Override
public void onPause()
{
    if(locationManager != null)
    {
        locationManager.removeUpdates(this);
    }

    super.onPause();
}

@Override
public void onResume()
{
    super.onResume();

    setUpMapIfNeeded();

    if(locationManager != null)
    {
        mMap.setMyLocationEnabled(true);
    }
}


/**
 * 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.basicMap)).getMap();
        // Check if we were successful in obtaining the map.

        //This is how you register the LocationSource
        mMap.setLocationSource(this);

        if (mMap != null) 
        {
            setUpMap();
        }
    }
}

/**
 * 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.
 * <p>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() 
{
    mMap.setMyLocationEnabled(true);
}

@Override
public void activate(OnLocationChangedListener listener) 
{
    mListener = listener;
}

@Override
public void deactivate() 
{
    mListener = null;
}

@Override
public void onLocationChanged(Location location) 
{
    if( mListener != null )
    {
        mListener.onLocationChanged( location );

        //Move the camera to the user's location once it's available!
        mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
    }
}

@Override
public void onProviderDisabled(String provider) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this, "provider disabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this, "provider enabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
{
    // TODO Auto-generated method stub
    Toast.makeText(this, "status changed", Toast.LENGTH_SHORT).show();
}
}
公共类MyLocationMapFragmentActivity扩展FragmentActivity实现LocationListener、LocationSource
{
/**
*请注意,如果Google Play services APK不可用,则该值可能为空。
*/
私有谷歌地图;
私有OnLocationChangedListener-mListener;
私人场所经理场所经理;
私有上下文;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_地图);
this.mContext=this;
locationManager=(locationManager)getSystemService(位置服务);
//您可能希望在此处传递另一个提供程序作为第一个参数
//取决于您希望的定位精度
//请参阅LocationManager.getBestProvider()
标准位置标准=新标准();
位置标准.设定精度(标准.精度\u罚款);
locationManager.RequestLocationUpdate(locationManager.getBestProvider(locationCriteria,true),1L,2F,this);
setupmapifneed();
}
@凌驾
公共无效暂停()
{
如果(locationManager!=null)
{
locationManager.RemoveUpdate(此);
}
super.onPause();
}
@凌驾
恢复时公开作废()
{
super.onResume();
setupmapifneed();
如果(locationManager!=null)
{
mMap.setMyLocationEnabled(真);
}
}
/**
*如果可能的话,设置地图(例如,Google Play services APK已正确启动)
*已安装)并且映射尚未实例化。这将确保
*当{@link#mMap}不为null时,调用{@link#setUpMap()}一次。
*
*如果未安装{@link SupportMapFragment}(和
*{@link com.google.android.gms.maps.MapView
*MapView})将提示用户在上安装/更新Google Play services APK
*他们的设备。
*
*用户可以按照提示正确返回此活动
*正在安装/更新/启用Google Play服务。因为活动可能尚未启动
*在此过程中完全破坏(可能只会停止或停止
*暂停),{@link#onCreate(Bundle)}可能不会再次调用,因此我们应该在中调用此方法
*{@link#onResume()}以保证将调用它。
*/
私有void setUpMapIfNeeded(){
//执行空检查以确认尚未实例化映射。
如果(mMap==null)
{
//尝试从SupportMapFragment获取映射。
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.basicMap)).getMap();
//检查我们是否成功获得地图。
//这是注册LocationSource的方式
mMap.setLocationSource(本);
如果(mMap!=null)
{
setUpMap();
}
}
}
/**
*在这里,我们可以添加标记或线条、添加侦听器或移动摄影机
*在非洲附近加一个标记。
*
*当我们确定{@link#mMap}不是null时,应该只调用一次。
*/
私有void setUpMap()
{
mMap.setMyLocationEnabled(真);
}
@凌驾
public void激活(OnLocationChangedListener侦听器)
{
mListener=监听器;
}
@凌驾
公开作废
{
mListener=null;
}
@凌驾
已更改位置上的公共无效(位置)
{
if(mListener!=null)
{
mListener.onLocationChanged(位置);
//一旦相机可用,将其移动到用户的位置!
mMap.animateCamera(CameraUpdateFactory.newLatLng(newLatLng(location.getLatitude(),location.getLongitude()));
}
}
@凌驾
公共无效onProviderDisabled(字符串提供程序)
{
//TODO自动生成的方法存根
Toast.makeText(该“提供程序已禁用”,Toast.LENGTH_SHORT).show();
}
@凌驾
公共无效onProviderEnabled(字符串提供程序)
{
//TODO自动生成的方法存根
Toast.makeText(这是“已启用提供程序”,Toast.LENGTH_SHORT).show();
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra)
{
//TODO自动生成的方法存根
Toast.makeText(此“状态已更改”,Toast.LENGTH_SHORT).show();
}
}
更新2

如果您有兴趣在用户位置离开屏幕时将地图置于用户位置的中心(就像旧API中MyLocationOverlay所做的那样),请参阅


或者这篇博文:

Hey Janusz-我的答案有问题吗?如果是,请告诉我,我会更新,否则请接受。