我试图在GoogleApiClient
的帮助下找到最后一个已知的位置我遵循了链接中的指南:
https://developer.android.com/training/location/retrieve-current.html
我在下面测试了我的代码,但是buildGoogleApiClient()
正在被调用,而不是onConnected()
。因此,未显示最后一个已知位置。
正在显示Google地图。
如何通过此方法获取最后一个已知位置?
地图活动:
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实现OnMapReadyCallback,ConnectionCallbacks,OnConnectionFailedListener { GoogleMap地图; 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);
}
}
}
答案 0 :(得分:1)
调用buildGoogleApiClient()后,您需要调用mGoogleApiClient.connect()。这应该使onConnected()得到调用。