2016-04-24 57 views
1

我想創建一個地圖活動,顯示用戶當前的位置,可以放大到街道級別,但我的應用程序無法正常工作,您認爲我需要的代碼是什麼加?地圖帶有實時位置更新的活動

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

應用程序正在運行,但地圖上沒有顯示,它沒有顯示我我當前的位置:(我希望有人能幫助我

+0

你沒有得到的地圖,因爲你沒有「得到的地圖」。您需要調用'getMapAsync()'並實現'onMapReadyCallBack()'回調[根據官方教程](https://developers.google.com/maps/documentation/android-api/intro)。也許有人會寫更詳細的解釋的適當答案。 –

回答

0

這裏是一步一步的,使用谷歌地圖:

A.configured項目與谷歌地圖合作(地圖可能不是如果沒有正確配置顯示):在「build.graddle」

  1. 用正確的版本如87年6月5日或8.1.0添加依賴: compile 'com.google.android.gms:play-services:8.1.0'
  2. 在 「Android.Manifest」

    添加權限,GL和API密鑰(確保把你APIKey):

    < uses-permission android:name="android.permission.WAKE_LOCK" /> < uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> < uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < uses-feature android:glEsVersion="0x00020000" android:required="true"/> <!-- Google Maps Fragment API Key Data --> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR API KEY" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

B.代碼:

  1. 使用OnMapReady:

    • 添加 「OnMapReadyCallback」 你的活動類: public class Map_Activity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener, OnMapReadyCallback {

    • 覆蓋onMapReady啓用位置,並有參考的GoogleMap:

      @Override public void onMapReady(GoogleMap map) { map.setMyLocationEnabled(true); googleMap = map; }

  2. 活動,OnCrate致電getMapAsync:

    FragmentManager fm = getSupportFragmentManager(); SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map); mapFragment.getMapAsync(this);

C.刪除/替換過​​時的代碼:

-LocationClient 
-replace "GooglePlayServicesClient" with GoogleApiClient