2016-05-24 92 views
0

我想實現谷歌地圖API。我希望在地圖上看到自己的位置,並且希望在我移動時有所改變。問題是沒有錯誤,但它不顯示我在地圖上的位置標記。這是我的onCreate()在java文件谷歌地圖Android API和當前位置

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapPane.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map_pane); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    // Create the LocationRequest object 
    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
      .setFastestInterval(1 * 1000); // 1 second, in milliseconds 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
}//onCreate 

梅索德這是我覺得我失去了一些東西

public void onConnected(@Nullable Bundle bundle) { 
    Log.i(TAG, "Location services connected."); 

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     Log.i(TAG, "loc exists"); 
     if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
       || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     }//if 
    }//if 
    else { 
     handleNewLocation(location); 
    }//else 
}//onConnected 

,因爲我從來沒有在第二if語句的方法。我想我首先需要用戶的許可?在我的設備上手動打開gps也不起作用。我用下面的方法在地圖上顯示標記

private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 
    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 
    LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

    MarkerOptions options = new MarkerOptions() 
      .position(latLng) 
      .title("I am here!"); 
    mMap.addMarker(options); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

}//handleNewLocation 

我用了更多的方法,但我認爲那裏沒有缺陷。在AndroidManifest.xml我包括

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
+0

什麼是地點的價值? –

+0

它是空的,因爲我可以看到「Log.i(TAG,」loc存在「);」在我的Android監視器 – Qas

+0

中,您是否像本教程一樣添加了onStart()和onStop()方法https://developer.android.com/training/location/retrieve-current.html? –

回答

0

您可以使用,而不是谷歌的API客戶端

   LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    final Geocoder gc = new Geocoder(this, Locale.getDefault()); 

    LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      //here you can get current position location.getLatitude(),location.getLongitude(); 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
0

看來你的代碼不給handlelocation()方法提供任何位置的LocationManager。

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapPane.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
LocationListener li; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map_pane); 
    // Obtain the SupportMapFragment and get notified when the map is reato be used. 
    SupportMapFragment mapFragment = (SupportMapFragment)   getSupportFragmentManager() 
     .findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

li=new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      handleNewLocation(location); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    getHandledCurrentLocation(); 
}//onCr 

private void getHandledCurrentLocation() { 
    String locationProvider = LocationManager.NETWORK_PROVIDER; 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    Log.e("LOCATION_DEBUG","LOCATION REQUESTED--- "); 
    LCM.requestLocationUpdates(locationProvider, 10 * 1000, 0,li); 

} 
private void handleNewLocation(Location location) { 
Log.d(TAG, location.toString()); 
double currentLatitude = location.getLatitude(); 
double currentLongitude = location.getLongitude(); 
LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

MarkerOptions options = new MarkerOptions() 
     .position(latLng) 
     .title("I am here!"); 
mMap.addMarker(options); 
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

}//handleNewLocation 

此代碼應該工作。

+0

如果它顯示任何錯誤,通知我.. –

0

我可以看到你的代碼有幾個問題。我不認爲你正在處理FusedLocationApi提供的位置更新。即使您的問題中未提供部分代碼,我相信您正在實施LocationListener。在這種情況下,您在您的活動中實施了一種方法'onLocationChanged(Location location)'。這是您應該嘗試在地圖上設置新位置的方法。您的handleNewLocation方法將不會由FusedLocationApi自動調用

您可能希望爲您的活動提供完整的代碼,然後我們可以幫助您更好地完成。

相關問題