Android 使用onConnected()显示最后一个已知位置

Android 使用onConnected()显示最后一个已知位置,android,google-maps,Android,Google Maps,我正试图借助GoogleAppClient到达最后一个已知位置,我遵循了链接中的指南: 我已经在下面测试了我的代码,并且调用了buildGoogleAppClient(),但没有调用onConnected()。因此,不显示最后已知的位置 谷歌地图正在显示 如何使用此方法获取最后一个已知位置 地图活动: public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnC

我正试图借助
GoogleAppClient
到达最后一个已知位置,我遵循了链接中的指南:

我已经在下面测试了我的代码,并且调用了
buildGoogleAppClient()
,但没有调用
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);                

        }

    }
}
编辑地图活动:

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);                

        }

    }
}
公共类映射扩展了FragmentActivity在MapreadyCallback、ConnectionCallbacks和OnConnectionFailedListener上的实现{ 谷歌地图; GoogleapClient MGoogleapClient; 位置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();

}
    @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);                

        }

    }
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
buildGoogleAppClient();
}
@凌驾
mapready上的公共无效(谷歌地图arg0){
mGoogleApiClient.connect();
System.out.println(“ABC onMapReady”);
}
私有布尔initMap(){
if(map==null){
SupportMapFragment MapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
map=mapFrag.getMap();
}
返回(map!=null);
}
@凌驾
受保护的void onNewIntent(意图){
super.onNewIntent(意向);
@抑制警告(“未选中”)
ArrayList list=(ArrayList)intent
.getSerializableExtra(“列表数据”);
for(ItemDTO ItemDTO:list){
int id=itemDTO.getBusId();
双纬度=itemDTO.getLatitude();
double longitude=itemDTO.getLongitude();
int route1=itemDTO.getRoute();
字符串route=Integer.toString(route1);
字符串方向=itemDTO.getDirection();
字符串route_dirc=route+“,”+方向;
if(initMap()){
地理位置(id、纬度、经度、路线);
}否则{
Toast.makeText(此“地图不可导航”,Toast.LENGTH\u SHORT)
.show();
}
}
}
受保护的同步无效BuildGoogleAppClient(){
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
println(“调用了ABC BuildGoogleAppClient映射:”);
}
@凌驾
未连接的公共空间(捆绑arg0){
mLastLocation=LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
如果(mLastLocation!=null){
double lng=mLastLocation.getLongitude();
双lat=mLastLocation.getLatitude();
如果(myLocatMarker!=null){
mylocatemarker.remove();
}
LatLng ll=新LatLng(lat,lng);
MarkerOptions markerOpt=新的MarkerOptions().title(“我的位置”)
.职位(ll)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
System.out.println(“ABC连接图:“+lat+”;“+lng”);
myLocatMarker=map.addMarker(markerOpt);
}
}
}

调用buildGoogleAppClient()后,需要调用mgoogleAppClient.connect()。这会使onConnected()被调用。

ref您的答案对我来说很有用,但为什么他们不在教程中使用它;)。但是当我点击后退按钮时,我在一个空对象引用上得到了
'com.google.android.model.Marker.com.google.android.gms.maps.GoogleMap.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)
Map.onConnected(Map.java:494)
您介意分享一些代码吗?**等等,我是多任务的-这从来不是一件好事。让我想一想,我打赌这与活动生命周期有关。我在想,当你转到你的位置时,会调用新的activity onPause(),当你点击后退按钮时,应该调用onResume()。尝试重新初始化myLocatMarker,我不确定这是否会解决它,因为我没有坐在您的计算机前,但我会查看您的发现。