Android:GoogleMap v2忽略了动画/移动呼叫 [英] Android: GoogleMap v2 ignores animate/move calls

查看:80
本文介绍了Android:GoogleMap v2忽略了动画/移动呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有GoogleMap的FragmentActivity.它可以正确地在onLocationChanged中接收用户位置,在这里我尝试使用用户位置来使地图居中:

I have a FragmentActivity with a GoogleMap inside. It correctly receives the user location in onLocationChanged, where I try to use the user location to center the map:

@Override
public void onLocationChanged(Location loc)
{
  if (centerMap && mMap != null)
  {
    LatLng location = new LatLng(loc.getLatitude(), loc.getLongitude());
    CameraPosition pos = new CameraPosition.Builder().target(location).zoom(12).build();
    CameraUpdate cu = CameraUpdateFactory.newCameraPosition(pos);
    mMap.animateCamera(cu);

    // centerMap = false;
    Log.e(getClass().getSimpleName(), "lat = " + location.latitude + " long = " + location.longitude);
  }
}

这段代码实际上不时地工作(也许只有一次),并且仅在调试会话期间起作用,在调试会话中,我在if语句中放置了一个断点.我不明白怎么了onLocationChanged方法会定期调用,它记录接收到的位置,这意味着它进入了if条件,但是地图距离lat = 0,long = 0初始位置(非洲)并没有移动一英寸.

This code actually worked now and then (maybe only once), and only during a debug session where I put a breakpoint inside the if statement. I don't understand what's wrong. The onLocationChanged method gets called regularly, it logs the position it received, that implies it entered the if condition, but the map does not move an inch from the lat=0,long=0 initial position (Africa).

有任何线索吗?

我的代码中必须有一些严重损坏的东西:即使标记不显示,这也是我将它们添加到地图中的方式

I must have something badly broken in my code: even markers do not show up, here is how I add them to the map

mMap.addMarker(new MarkerOptions()
                      .position(new LatLng(lat, lng))
                      .title("title")
                      .draggable(false)
                      .visible(true));

即使我打了电话,也没有显示我的位置"图标

and the "My location" icon is not showing up either, even if I called

mMap.setMyLocationEnabled(true);

在onCreate()中.为了排除火星人,我已经将Eclipse,ADT和所有其他更新为最新的可用版本.

in onCreate(). Trying to exclude martians and such, I've already updated Eclipse, ADT and all the rest to the latest available versions.

推荐答案

由于某种原因(我不知道),将地图初始化从onCreate()移到对onLocationChange()的第一次调用中就可以了.现在我的onCreate()很简单

For some reason (unknown to me), moving the map initialization from onCreate() into the first invocation of onLocationChange() did the trick. Now my onCreate() is simply

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

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);

  setContentView(R.layout.map_activity);

}

我的onStart()是:

my onStart() is:

@Override
protected void onStart()
{
  super.onStart();

  // Create the LocationRequest object
  mLocationRequest = LocationRequest.create();
  // Use high accuracy
  mLocationRequest.setPriority(
        LocationRequest.PRIORITY_HIGH_ACCURACY);
  // Set the update interval to 5 seconds
  mLocationRequest.setInterval(UPDATE_INTERVAL);
  // Set the fastest update interval to 1 second
  mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

   mLocationClient = new LocationClient(this, this, this);
   mLocationClient.connect();
}

@Override
public void onConnected(Bundle arg0)
{
  mLocationClient.requestLocationUpdates(mLocationRequest, this);  
}

我的onLocationChange()是

and my onLocationChange() is

@Override
public void onLocationChanged(Location loc)
{
  if (mMap == null)
  {
    mapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
        .beginTransaction();
    fragmentTransaction.add(R.id.mapcontainer, mapFragment);
    fragmentTransaction.commit();

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    mMap.getUiSettings().setAllGesturesEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.setMyLocationEnabled(false);

    LatLng location = new LatLng(loc.getLatitude(), loc.getLongitude());
    CameraPosition pos = new CameraPosition.Builder().target(location).zoom(12).build();
    CameraUpdate cu = CameraUpdateFactory.newCameraPosition(pos);
    mMap.animateCamera(cu);

    if (mLocationClient.isConnected())
      mLocationClient.removeLocationUpdates(this);
    mLocationClient.disconnect();

    Log.e(getClass().getSimpleName(), "lat = " + location.latitude + " long = " + location.longitude);
  }
}

它可以工作,但是现在我遇到了另一个问题(地图似乎忽略了触摸事件).我将为此创建一个单独的问题.

It works, but now I have a different problem (the map seems to ignore touch events). I'm going to create a separate question for that.

这篇关于Android:GoogleMap v2忽略了动画/移动呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆