【问题标题】:Display the last known location with onConnected()使用 onConnected() 显示最后一个已知位置
【发布时间】:2015-11-23 03:41:13
【问题描述】:

我正在尝试借助 GoogleApiClient 到达最后一个已知位置,我按照链接中的指南进行操作:

https://developer.android.com/training/location/retrieve-current.html

我已经在下面测试了我的代码,buildGoogleApiClient() 被调用但不是onConnected()。因此,不会显示最后一个已知位置。

正在显示谷歌地图。

如何使用这种方法获取最后一个已知位置?

地图活动:

public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        buildGoogleApiClient();
}

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
         System.out.println("ABC buildGoogleApiClient map was invoked: ");
    }
    @Override
    public void onConnected(Bundle arg0) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
             double lng = mLastLocation.getLongitude();
             double lat = mLastLocation.getLatitude();

            if(myLocatMarker != null){
                myLocatMarker.remove();
            }
             LatLng ll = new LatLng(lat, lng);
             MarkerOptions markerOpt = new MarkerOptions().title("my location")
                     .position(ll)
                     .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
             System.out.println("ABC onConnected map: "+ lat + " ; " + lng);
             myLocatMarker = map.addMarker(markerOpt);                

        }

    }
}

编辑地图活动:

公共类 Map 扩展 FragmentActivity 实现 OnMapReadyCallback、ConnectionCallbacks、OnConnectionFailedListener{ 谷歌地图; GoogleApiClient mGoogleApiClient; 位置 mLastLocation; 标记 myLocatMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        buildGoogleApiClient();

}
    @Override
public void onMapReady(GoogleMap arg0) {
            mGoogleApiClient.connect(); 
    System.out.println("ABC onMapReady");
}
private boolean initMap() {
    if (map == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        map = mapFrag.getMap();
    }
    return (map != null);
}
    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    @SuppressWarnings("unchecked")
    ArrayList<ItemDTO> list = (ArrayList<ItemDTO>) intent
            .getSerializableExtra("list_data");
    for (ItemDTO itemDTO : list) {
        int id = itemDTO.getBusId();
        double latitude = itemDTO.getLatitude();
        double longitude = itemDTO.getLongitude();
        int route1 = itemDTO.getRoute();
        String route = Integer.toString(route1);
        String direction = itemDTO.getDirection();
        String route_dirc = route + ", " + direction;

        if (initMap()) {
            gotoLocation(id, latitude, longitude, route_dirc);                                              

        } else {
            Toast.makeText(this, "Map not avialable", Toast.LENGTH_SHORT)
                    .show();
        }

    }
}

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
         System.out.println("ABC buildGoogleApiClient map was invoked: ");
    }
    @Override
    public void onConnected(Bundle arg0) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
             double lng = mLastLocation.getLongitude();
             double lat = mLastLocation.getLatitude();

            if(myLocatMarker != null){
                myLocatMarker.remove();
            }
             LatLng ll = new LatLng(lat, lng);
             MarkerOptions markerOpt = new MarkerOptions().title("my location")
                     .position(ll)
                     .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
             System.out.println("ABC onConnected map: "+ lat + " ; " + lng);
             myLocatMarker = map.addMarker(markerOpt);                

        }

    }
}

【问题讨论】:

标签: android google-maps


【解决方案1】:

调用 buildGoogleApiClient() 后,您需要调用 mGoogleApiClient.connect()。这应该会调用 onConnected()。

【讨论】:

  • 你的答案对我有用,但为什么他们没有,但在教程中;)。
  • 但是当我点击后退按钮时,我得到了'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
  • Map.onConnected(Map.java:494)
  • 您介意分享一些代码吗? ** 等一下,我正在处理多项任务——这绝不是一件好事。让我考虑一下。
  • 我敢打赌这与活动生命周期有关。我在想当你去你的新活动时 onPause() 被调用,当你点击后退按钮时 onResume() 应该被调用。尝试重新初始化 myLocatMarker,我不确定这是否会解决它,因为我没有坐在你的电脑前,但我会回来看看你发现了什么。
猜你喜欢
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
相关资源
最近更新 更多