gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:38 26 4
gpt4 key购买 nike

我可以通过 setMyLocationEnabled 在新的 Maps Api 中启用 myLocation View .

Api 的第一个版本有一个方法 runOnFirstFix这使我能够在找到位置后将 map 动画化到用户位置。我在 Api 版本 2 中找不到类似的监听器或位置。

是否有一种解决方案可以在用户定位后执行操作?

最佳答案

Google Maps Android API V2 有一个 LocationSource。来自文档:

“GoogleMap 对象为其 my-location 层提供了一个内置位置提供程序,但它可以替换为另一个实现此接口(interface)的提供程序。”

LocationSource

我想您需要将它与 LocationSource.OnLocationChangedListener 结合使用

更新

想通了。如果您想执行类似于 runOnFirstFix 的操作,这里有一个基本示例,它会等待用户的位置可用,然后将 map 设置为以用户位置为中心的动画。

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();
}
}

更新 2

如果您有兴趣在用户位置离开屏幕时将 map 居中放置在用户位置上(就像旧 API 中的 MyLocationOverlay 所做的那样),请参阅 this answer

或这篇博文:Google Maps Android API V2 MyLocation LocationSource and event handling

关于android - Android Maps Api V2 中是否有类似于 onFirstFix 方法的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13704154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com