gpt4 book ai didi

android - 跟随用户的 map View - Android Maps API v2 的 MyLocationOverlay 类型功能

转载 作者:IT老高 更新时间:2023-10-28 23:29:31 25 4
gpt4 key购买 nike

我切换到 Android 版 map v2,并尝试移植以下功能:

使用 MyLocationOverlay,我可以显示设备的当前位置(蓝点)。当用户的位置发生变化并且点到达可见区域的边缘时, map 会实时显示动画,因此点会成为 View 的中心。

在 v2 中,我将 SupportMapFragment 与 getMap().setMyLocationEnabled(true) 一起使用。当前位置显示为一个蓝点(箭头),但当设备更改位置时, map View 不会移动,并且该点最终会离开 View 。

有什么想法吗?

最佳答案

您需要将 LocationSource 添加到您的 GoogleMap 并响应 onLocationChanged 事件。这是一个简单的类,它询问用户的位置,然后等到用户的位置可用,然后将 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;

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

setContentView(R.layout.basic_map);

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

if(locationManager != null)
{
boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(gpsIsEnabled)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);
}
else if(networkIsEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this);
}
else
{
//Show an error dialog that GPS is disabled.
}
}
else
{
//Show a generic error dialog since LocationManager is null for some reason
}

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.

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

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

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

这应该跟随用户,并在 map 发生变化时保持 map 居中 - 如果您只想在用户“离开屏幕”时将 map 居中放在用户身上,那么您可以检查用户的位置在 map 的可见范围内

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

LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

if(!bounds.contains(new LatLng(location.getLatitude(), location.getLongitude())))
{
//Move the camera to the user's location if they are off-screen!
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
}
}

以下是有关该主题的简短文章: Google Maps Android API V2 MyLocation LocationSource and event handling

更新

我编辑了获取用户位置的方式(请参阅 onCreate 中的 locationManager 代码)。我发现使用“getBestProvider”方法并不可靠,而且不能在多个设备上工作。我有几个用户提示他们的设备永远找不到他们的位置,无论我制定的标准多么宽松。看来手动选择 GPS 或网络是通用的。

关于android - 跟随用户的 map View - Android Maps API v2 的 MyLocationOverlay 类型功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739990/

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