我正在努力使用Android Google Maps API的animateCamera方法,正在发生的事情是它只运行一次。当我回到活动一段时间后它根本不工作。我不知道为什么它在我打开活动的第一次工作时没有工作但是之后它根本不工作,我调试代码并检查是否抛出任何错误但没有捕获错误。即使是animateCamera方法调用也可以顺利运行,但是在重新返回活动后,地图没有动画到位置。
public class MapsActivity extends Activity {
private static GoogleMap mGoogleMap = null;
private static final String TAG = "MapsActivity";
private Button mBtnStartRide, mBtnPauseRide, mBtnStopRide = null;
private static TextView mTxtLatLong, mTxtTimer, mTxtTotalSize,
mTxtSpeed = null;
private static PolylineOptions mRectLine = null;
// Storing the directions returned by the direcction api
private static ArrayList<LatLng> mDirectionsPoints = new ArrayList<LatLng>();
private SharedPreferences mPreferences = null;
private MyBroadcastReceiver receiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
receiver = new MyBroadcastReceiver();
mPreferences = getSharedPreferences(Preferences.PREFERENCES,
Context.MODE_PRIVATE);
mTxtLatLong = (TextView) findViewById(R.id.txtLatLong);
mTxtTimer = (TextView) findViewById(R.id.txtTimer);
mTxtTotalSize = (TextView) findViewById(R.id.txtDirectionsSize);
mTxtSpeed = (TextView) findViewById(R.id.txtSpeed);
mBtnStartRide = (Button) findViewById(R.id.btn_start_ride);
mBtnPauseRide = (Button) findViewById(R.id.btn_pause_ride);
mBtnStopRide = (Button) findViewById(R.id.btn_stop_ride);
mBtnStartRide.setOnClickListener(btnStartRideClickListener);
mBtnPauseRide.setOnClickListener(btnPauseRideClickListener);
mBtnStopRide.setOnClickListener(btnStopRideClickListener);
initilizeMap();
}
/**
* Start Ride Button Click Listener
*/
private OnClickListener btnStartRideClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mPreferences.getBoolean(Preferences.IS_RIDE_PAUSE, false)) {
if (RidingTimerService.getInstance() != null) {
RidingTimerService.getInstance().reStartRide();
}
} else {
startService(new Intent(MapsActivity.this,
RidingTimerService.class));
}
mPreferences.edit()
.putBoolean(Preferences.IS_RIDE_STOPPED, false)
.commit();
mBtnStartRide.setVisibility(View.GONE);
mBtnPauseRide.setVisibility(View.VISIBLE);
mBtnStopRide.setVisibility(View.VISIBLE);
}
};
/**
* Start Ride Button Click Listener
*/
private OnClickListener btnPauseRideClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
RidingTimerService.getInstance().pauseRide();
mPreferences.edit().putBoolean(Preferences.IS_RIDE_PAUSE, true)
.commit();
mBtnStartRide.setVisibility(View.VISIBLE);
mBtnPauseRide.setVisibility(View.GONE);
mBtnStopRide.setVisibility(View.VISIBLE);
}
};
/**
* Stop Ride Button Click Listener
*/
private OnClickListener btnStopRideClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MapsActivity.this,
RidingTimerService.class));
mPreferences.edit().remove(Preferences.IS_RIDE_PAUSE).commit();
mPreferences.edit()
.putBoolean(Preferences.IS_RIDE_STOPPED, true).commit();
mDirectionsPoints.clear();
mBtnStartRide.setVisibility(View.VISIBLE);
mBtnPauseRide.setVisibility(View.GONE);
mBtnStopRide.setVisibility(View.GONE);
}
};
@Override
protected void onResume() {
super.onResume();
registerReceiver(receiver, new IntentFilter(
Constants.BROADCAST_INTENT));
initilizeMap();
if (mPreferences.getBoolean(Preferences.IS_RIDE_STOPPED, false)) {
// Show start button and gone Pause & Stop both
mBtnStartRide.setVisibility(View.VISIBLE);
mBtnPauseRide.setVisibility(View.GONE);
mBtnStopRide.setVisibility(View.GONE);
} else if (mPreferences
.getBoolean(Preferences.IS_RIDE_PAUSE, false)) {
mBtnStartRide.setVisibility(View.VISIBLE);
mBtnPauseRide.setVisibility(View.GONE);
mBtnStopRide.setVisibility(View.VISIBLE);
} else {
mBtnStartRide.setVisibility(View.GONE);
mBtnPauseRide.setVisibility(View.VISIBLE);
mBtnStopRide.setVisibility(View.VISIBLE);
}
setAllText();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
/**
* Function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (mGoogleMap == null) {
mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (mGoogleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
} else {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
mGoogleMap.getUiSettings().setCompassEnabled(true);
mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mRectLine = new PolylineOptions().width(6).color(Color.BLUE);
}
} else {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
mGoogleMap.getUiSettings().setCompassEnabled(true);
mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mRectLine = new PolylineOptions().width(6).color(Color.BLUE);
animateCameraTo(mDirectionsPoints.get(0).latitude,
mDirectionsPoints.get(0).longitude);
}
}
/**
* Handler which handle the timer of ride
*/
public static Handler mCountDownHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Log.i(TAG, msg.getData().getString("time"));
mTxtTimer.setText("Timer - " + msg.getData().getString("time"));
};
};
/**
* @author Scorpion
*
*/
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mDirectionsPoints.add(new LatLng(intent.getExtras().getDouble(
Preferences.LATITUDE), intent.getExtras().getDouble(
Preferences.LONGITUDE)));
setAllText();
if (!mDirectionsPoints.isEmpty() && mDirectionsPoints.size() == 1) {
mGoogleMap.addMarker(new MarkerOptions()
.position(mDirectionsPoints.get(0)).anchor(0.8f, 1.0f)
.title("Your Location"));
}
animateCameraTo(
intent.getExtras().getDouble(Preferences.LATITUDE),
intent.getExtras().getDouble(Preferences.LONGITUDE));
}
}
/**
*
*/
public void setAllText() {
mTxtLatLong.setText("Lat - "
+ mPreferences.getString(Preferences.LATITUDE, "0.0")
+ ", Lng - "
+ mPreferences.getString(Preferences.LONGITUDE, "0.0"));
mTxtSpeed.setText("Speed - "
+ mPreferences.getString(Preferences.SPEED, "0.0"));
mTxtTotalSize.setText("Distance - "
+ mPreferences.getString(Preferences.DISTANCE, "0.0"));
}
/**
* Animate to position on Google Maps
*
* @param lat
* @param lng
*/
private void animateCameraTo(final double lat, final double lng) {
// Saving the points in a polyline
for (int i = 0; i < mDirectionsPoints.size(); i++) {
// Elevation array
mRectLine.add(mDirectionsPoints.get(i));
}
// Drawing the path on the map
mGoogleMap.addPolyline(mRectLine);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
new LatLng(lat, lng), 17);
mGoogleMap.animateCamera(cameraUpdate);
}
}
还试过这个: -
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(17.385044, 78.486671)).zoom(12).build();
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
我希望每当用户再次回到此活动时,他也应该能够看到路径和位置标记。我存储了所有lat长度,这对我来说不是问题,但是现在还没有使用animateCamera和标记。首次使用谷歌地图v2,所以不确定究竟是什么问题。期待获得一些快速帮助或提示。
答案 0 :(得分:0)
试试这个:
创建自己的类,如:
public class MapUtil {
public static void animateMapToPositionAndZoom(GoogleMap googleMap, LatLng position, float zoomLevel) {
Log.i("animate map to position and zoom","Camera update on location change inclucding zoom");
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(position, zoomLevel);
googleMap.moveCamera(cameraUpdate);
}
}
在您的活动中覆盖此内容:
private LatLng myLatLng;
@Override
public void onConnected(Bundle arg0) {
if (myLatLng!= null) {
MapUtil.animateMapToPositionAndZoom(googleMap, myLatLng, currentZoomLevel);
}
}
它可以帮助你。
答案 1 :(得分:0)
你好,你可以使用SharedPreference然后缓存位置。当重新打开app时你可以调用“animateCamera()”。重要性,在位置变更时保存位置。
public View onCreateView(final LayoutInflater layoutInflater, final ViewGroup viewGroup, final Bundle bundle) {
SupportMapFragment.newInstance();
...
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(1000);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// same SharedPreference
String model = SharedManager.getInstant(activity).getModel(Constant.CACHE_LOCATION);
if(model == null || model.isEmpty()){
return;
}
try {
LatLng latLng = new Gson().fromJson(model, LatLng.class);
animateCamera(latLng);
} catch (Exception e) {
}
}
});
}
}).start();
return inflate;
}
private void animateCamera(LatLng latLng) {
final CameraPosition cameraPosition = new CameraPosition.Builder().target(
latLng).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
new Handler().post(new Runnable() {
@Override
public void run() {
// todo something
}
});
}
@Override
public void onCancel() {
}
});
}