我想創建一個地圖活動,顯示用戶當前的位置,可以放大到街道級別,但我的應用程序無法正常工作,您認爲我需要的代碼是什麼加?地圖帶有實時位置更新的活動
public class Map_Activity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener{
private GoogleMap myMap;
private LocationClient myLocationClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000)
.setFastestInterval(16)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getMapReference();
}
@Override
protected void onResume(){
super.onResume();
getMapReference();
wakeUpLocationClient();
myLocationClient.connect();
}
@Override
public void onPause(){
super.onPause();
if (myLocationClient != null){
myLocationClient.disconnect();
}
}
private void gotoMyLocation(double lat, double lng) {
changeCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(lat, lng))
.zoom(15.5f)
.bearing(0)
.tilt(25)
.build()
), new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
}
@Override
public void onCancel() {
}
});
}
private void wakeUpLocationClient(){
if(myLocationClient == null){
myLocationClient = new LocationClient(getApplicationContext(),this,this);
}
}
private void getMapReference(){
if(myMap==null){
myMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
if (myMap != null){
myMap.setMyLocationEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}
@Override
public void onConnected(Bundle bundle){
myLocationClient.requestLocationUpdates(REQUEST, this);
}
@Override
public void onDisconnected(){
}
@Override
public void onLocationChanged(Location location){
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult){
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void changeCamera(CameraUpdate update, GoogleMap.CancelableCallback callback) {
myMap.moveCamera(update);
}
}
應用程序正在運行,但地圖上沒有顯示,它沒有顯示我我當前的位置:(我希望有人能幫助我
你沒有得到的地圖,因爲你沒有「得到的地圖」。您需要調用'getMapAsync()'並實現'onMapReadyCallBack()'回調[根據官方教程](https://developers.google.com/maps/documentation/android-api/intro)。也許有人會寫更詳細的解釋的適當答案。 –