0

誰能幫我Please.I不知道是什麼happened.My位置是工作..SetMyLocationEnabled不是在Android的地圖工作

我已經加入了API密鑰和知道事事代碼展示給我的當前位置它到目前爲止,但突然它不是>?

任何一個可以幫助請

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

private GoogleMap mMap; 
private final static int MY_PERMISSION_FINE_LOCATION = 101; 
ZoomControls zoom; 
Button markBt; 
Button geoLocationBt; 
Button satView; 
Button clear; 
Double myLatitude = null; 
Double myLongitude = null; 
private GoogleApiClient googleApiClient; 
private LocationRequest locationRequest; 
protected static final String TAG = "MapsActvity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // 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); 

    googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

    locationRequest = new LocationRequest(); 
    locationRequest.setInterval(15 * 1000); 
    locationRequest.setFastestInterval(5 * 1000); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    //set to balanced power accuracy on real device 


} 


/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(-34, 151); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 
     @Override 
     public void onMapClick(LatLng latLng) { 
      mMap.addMarker(new MarkerOptions().position(latLng).title("from onMapClick")); 
      mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
     } 
    }); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_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. 
     mMap.setMyLocationEnabled(true); 
    } else { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION); 
     } 
    } 


} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 
     case MY_PERMISSION_FINE_LOCATION: 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
        mMap.setMyLocationEnabled(true); 
       } 

      } else { 
       Toast.makeText(getApplicationContext(), "This app requires location permissions to be granted", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
      break; 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    requestLocationUpdates(); 
} 

private void requestLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
    } 

} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "Connection Suspended"); 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    Log.i(TAG, "Connection Failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode()); 
} 

@Override 
public void onLocationChanged(Location location) { 
    myLatitude = location.getLatitude(); 
    myLongitude = location.getLongitude(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    googleApiClient.connect(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
     LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); 
} 


@Override 
protected void onResume() { 
    super.onResume(); 
    if (googleApiClient.isConnected()) { 
     requestLocationUpdates(); 
    } 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    googleApiClient.disconnect(); 
} 

}

+0

請確保您有精確的位置信息的權限你的清單。你可以在這裏瞭解更多關於它的信息(https://developers.google.com/maps/documentation/android-api/location#location_permissions)。您也可以在文檔的下一步中檢查如何在代碼中正確添加或實現'setMyLocationEnabled(true)'。有關此問題的更多信息,請查看以下相關的SO問題:[map.setMyLocationEnabled(true);是不工作](http://stackoverflow.com/questions/39395739)和[map.setmylocationenabled(true)不工作](http://stackoverflow.com/questions/34608517) – KENdi

+0

在清單中添加,我也添加map.setMyLocationEnabled(真); –

回答

0

哎呀.....我的GPS不能正常工作......這就是問題