Android - 通过 Google 地图获取当前位置

Android - getting current location through Google Map

我正在开发与 Google 地图相关的应用程序。我已成功完成以下步骤。

  1. 创建了 API 访问 Google 地图的密钥
  2. 在我的应用程序中添加了 Google Play 服务库
  3. 添加了所需的权限
  4. SupportMapFragment
  5. 在我的 activity 中添加了地图
  6. 添加了单独的 class MyMap.java 来操作地图
  7. 已将两个参数传递给 class - activity 的 ContextGoogleMap
  8. 的对象
  9. 打开 Wi-Fi 和 GPS 并运行应用程序

在此之后,我得到了具有漂亮外观和控件的地图。

MyMap.java

public class MyMap implements ConnectionCallbacks, OnConnectionFailedListener {

    private Context context;
    private GoogleMap map;
    private GoogleApiClient client = null;

    public MyMap(Context context, GoogleMap map) {
        this.context = context;
        this.map = map;

        client = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
    }



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

    }

    @Override
    public void onConnected(Bundle arg0) {

        Toast.makeText(context, "Connected", 1).show();

        Location mLastLocation = LocationServices.FusedLocationApi
                .getLastLocation(client);

        if (mLastLocation != null) {

            Toast.makeText(
                    context,
                    String.valueOf(mLastLocation.getLatitude()) + ","
                            + String.valueOf(mLastLocation.getLongitude()), 1)
                    .show();
        }

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

}

问题

在上面class我要敬酒当前位置。但它没有敬酒任何东西。至少我需要在 onConnected 上看到祝酒词 "connected" 事件。我的实现有问题吗?

提前致谢。

您似乎从来没有 connect 您的客户,所以如果 onConnected 被调用,那将是一个真正的惊喜:)

您使用

创建您的客户端
 client = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();

但要让客户端执行某些操作,您必须添加:

client.connect(); 

getLastLocation() 只会给出一次位置。要获取定期位置更新,您需要重写 onLocationChanged() 方法。你可以得到这个 Link

我发现的最佳方法 是简单的实现你 activity 像这样:

public class MapActivity extends Activity implements GoogleMap.OnMyLocationChangeListener 

并覆盖方法

@Override
public void onMyLocationChange(Location location) {
    mMap.addMarker(new MarkerOptions().position(location).icon(
                BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}

并且不要忘记 map init 方法中的 mMap.setMyLocationEnabled(true);mMap.setOnMyLocationChangeListener(this);

就这些了!

另外,您可以像这里一样查看地图是否可用:

public boolean checkMapsAvailable() {
    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (isAvailable == ConnectionResult.SUCCESS) {
        return true;
    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, 9001);
        dialog.show();
    } else {
        Constants.showToast(Constants.ALERT_GOOGLEPLAY_CONNECTION);
    }
    return false;
}

希望这对您有所帮助。