gpt4 book ai didi

android - 谷歌地图航向标记或指示器

转载 作者:行者123 更新时间:2023-11-29 00:26:40 24 4
gpt4 key购买 nike

Jellybean 中的“Google map ”应用程序有一个指示器,它会根据您面对的方向进行调整。我有兴趣为多个指标实现类似的指标。

有没有人知道他们是如何实现航向指示器的(请注意,即使在 map 旋转时它也会保持航向)。我尝试使用我自己的标记方法,然后我意识到标记不会随 map 旋转(经过几个小时的劳动后有点糟糕......)

总结一下:如何在谷歌地图中实现一个基于航向而不是 map 旋转的图标。

到目前为止,我看到了两个解决方案:

  • 捕获 map 旋转并将其传输到标记
  • 使用 OpenGL 在 map 上绘制

在我开始另一个兔子洞之前,有人可以提供任何建议吗?谢谢!

最佳答案

如果您想在 map 上显示方向,您可以做几件事。

这里是所有使用的导入:

import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;

1.移动 map ,使方位在上面。

private final GoogleMap mMap;

//Get the current position from the map
CameraPosition camPosition = mMap.getCameraPosition();

//Update only the orientation/bearing, get all of the other values from the existing position.
CameraPosition newPos = new CameraPosition(
camPosition.target,
camPosition.zoom,
camPosition.tilt,
azimuthInDegress);

//Set the position so that the map will update. The desired heading should not be on top
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(newPos));

这个想法的问题在于,如果您不稳定方向更新, map 就会到处移动。

2.实现一个LocationSource

Google map 使您能够覆盖其位置提供程序。首先,您需要实现一个新的位置源:

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.MapFragment;


public class LocationSourceImplActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//Assume using the example from Google
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();

map.setMyLocationEnabled(true);

LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

map.setLocationSource(new LocationSourceImpl(locManager));
}

/**
*The Goal of this class is to listen for both location and heading updates
*and combine these into a single update, which will be passed to Google Maps.
*/
private class LocationSourceImpl implements LocationSource, LocationListener {

private OnLocationChangedListener mListener = null;
private Location mLastLoc = null;
//Use your desired location provider. You can also update this class to better
//determine when and how to register
private static final String PREF_LOC_SRC ="";
private final LocationManager mLocationManager;
private float mLastHeading = Float.MAX_VALUE;


public LocationSourceImpl(LocationManager pLocManager) {
mLocationManager = pLocManager;

//Get all updates, modify as desired.
mLocationManager.requestLocationUpdates(PREF_LOC_SRC, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) {
mLastLoc = location;
notifyListener();
}

private void notifyListener() {

if(mListener != null && mLastLoc!=null) {

if(mLastHeading != Float.MAX_VALUE) {
mLastLoc.setBearing(mLastHeading);
}

//The listener will be the maps:
mListener.onLocationChanged(mLastLoc);

}

}

@Override
public void activate(OnLocationChangedListener pListener) {
//When the setLocationSource is called on the map, the map
//will be passed in as an OnLocationChangedListener (may not be the
//map, but may be a delegator created by the map.
mListener = pListener;
}

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

}

/**
* This method can/should be replaced with a heading calculation
* @param pDegrees
* Heading in degrees
*/
public void updateHeadingInDegrees(float pDegrees) {
mLastHeading = pDegrees;
notifyListener();

}

@Override
public void onProviderDisabled(String paramString) {

}

@Override
public void onProviderEnabled(String paramString) {

}

@Override
public void onStatusChanged(String paramString, int paramInt,
Bundle paramBundle) {

}

}

关于android - 谷歌地图航向标记或指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18858720/

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