【发布时间】: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