使用locationclient在当前位置启动Google地图

时间:2014-08-06 20:26:31

标签: android google-maps google-maps-android-api-2 location-client

我试图让Google地图从用户当前位置开始。现在它开始一直缩放。我有正确的代码来查找和动画相机到用户的位置,但这个代码只有当我把它放在一个按钮并自己触摸它时才有效。如何在活动开始时让它移动?

我一直在尝试很多不同的事情。这是其中之一。

public class FindBar extends Activity implements ConnectionCallbacks,
    OnConnectionFailedListener, LocationListener,
    OnMyLocationButtonClickListener {

private GoogleMap mMap;
private LocationClient mLocationClient;
//  Button test;
boolean locationClientConnected = false;


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    setUpMapIfNeeded();
    setUpLocationClientIfNeeded();
    mLocationClient.connect();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if (mLocationClient != null) {
        mLocationClient.disconnect();
        Log.d("tag", "locationclient disconnected");
    }
}

private void setUpMapIfNeeded() {
    Log.d("tag", "entered set up map");

    // Check if we were successful in obtaining the map.
    if (mMap != null) {
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationButtonClickListener(this);
        Log.d("tag", "map location set true and click listener set");
    }

}

private void setUpLocationClientIfNeeded() {
    if (mLocationClient == null) {
        mLocationClient = new LocationClient(getApplicationContext(), this, // ConnectionCallbacks
                this); // OnConnectionFailedListener
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // removes the title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.fragment_find_bar);

    // Get a handle to the Map Fragment
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

//      test = (Button) findViewById(R.id.bTest);
//      test.setOnClickListener(new OnClickListener() {
//
//          @Override
//          public void onClick(View v) {
//              // TODO Auto-generated method stub
//              moveCameraTo(mMap.getMyLocation());
//          }
//
//      });

    moveCameraTo(mLocationClient.getLastLocation());
}

// Takes a Location and centers camera on that location
public void moveCameraTo(Location mLocation) {

    Log.d("camera", "entered moveToCamera");
    LatLng camPos = new LatLng(mLocation.getLatitude(),
            mLocation.getLongitude());
    Log.d("camera", "created camera position");

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camPos, 15));
    Log.d("camera", "moved camera");
}

@Override
public boolean onMyLocationButtonClick() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub
    Log.d("camera", "entered onConnected");
    // moveCameraTo(mMap.getMyLocation());
    locationClientConnected = true;
}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

   }

}

1 个答案:

答案 0 :(得分:0)

此代码几乎来自样本。如果从getLastLocation()获得非null位置,则将该位置插入setupmap方法。

if (mMap == null) {

            // Try to obtain the map from the SupportMapFragment.
            // mMap = mMapFragment.getMap();
            // // Check if we were successful in obtaining the map.

            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            // use the settings maptype

            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
                // Start Parsing the map
            }
        }


private void setUpMap() {

    final View mapView = getSupportFragmentManager().findFragmentById(
            R.id.map).getView();
    if (mapView.getViewTreeObserver().isAlive()) {
        mapView.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    // We use the new method when supported
                    @SuppressLint("NewApi")
                    // We check which build version we are using.
                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            mapView.getViewTreeObserver()
                                    .removeGlobalOnLayoutListener(this);
                        } else {
                            mapView.getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        }
                        // 
                        // put your move code here replacing code below
                        // which moves the map.
                        //
                        if (currentCameraPosition != null) {
                            mMap.moveCamera(CameraUpdateFactory
                                    .newCameraPosition(currentCameraPosition));
                            } else {
                            // else move to bounds of the map
                            if (bounds != null) {
                                mMap.moveCamera(CameraUpdateFactory
                                        .newLatLngBounds(bounds, 50));
                            }
                        }
                      }
                });
    }
}