如何使用NOX Emulator在Android Studio中获取当前位置?我的地图已显示并正确显示,我没有问题,但是我当前的位置错误。它将地点设置为加利福尼亚的洛杉矶,我来自菲律宾。我该如何解决?
这是我的代码的内容:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.usnews.com/rss/the-report. (Reason: CORS header ‘access-control-allow-origin’ missing).
这是我的AndroidManifest.xml文件:
@Override
public void onmapReady(GoogleMap googleMap) {
Log.d(TAG,"onmapReady: Map is Ready");
mMap = googleMap;
if(mLocationPermssionGranted){
getDeviceLocation();
if(activityCompat.checkSelfPermission(this,Manifest.permission.accESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && activityCompat.checkSelfPermission(this,Manifest.permission.accESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
private static final String TAG = "CommuterHomeactivity";
//This message will return to the user if uses wrong google play services version
private static final int ERROR_DIALOG_REQUEST = 9001;
//Declare variable to handle the id of Bottom Navigation View Palette
BottomNavigationView bottomNavigationView;
private static final String FINE_LOCATION = Manifest.permission.accESS_FINE_LOCATION;
private static final String COARSE_LOCATION = Manifest.permission.accESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private static final float DEFAULT_zoom = 15f;
//variables
private Boolean mLocationPermssionGranted = false;
private GoogleMap mMap;
private FusedLocationProviderClient mfusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_commuter_home);
MultiDex.install(this);
bottomNavigationView = findViewById(R.id.bottomNavBar);
//Set clicked navigation as active throught bolding the icon when a user currently on that page
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
if(isGooglePlayVersionCompatible()){
initMap();
}
getLocationPermission();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.ic_history:
Intent intent2 = new Intent(CommuterHomeactivity.this,CommuterTravelHistoryactivity.class);
startactivity(intent2);
break;
case R.id.ic_book:
Intent intent3 = new Intent(CommuterHomeactivity.this,CommuterBookRideactivity.class);
startactivity(intent3);
break;
case R.id.ic_account:
Intent intent4 = new Intent(CommuterHomeactivity.this,CommuterProfileactivity.class);
startactivity(intent4);
break;
case R.id.ic_sign:
Intent intent5 = new Intent(CommuterHomeactivity.this,CommuterTrafficRulesactivity.class);
startactivity(intent5);
break;
}
return false;
}
});
}
private void getDeviceLocation(){
Log.d(TAG,"getDeviceLocation: Getting the device current location");
mfusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try{
if(mLocationPermssionGranted)
{
Task location = mfusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Location currentLocation = (Location) task.getResult();
Log.d(TAG,"onComplete: Found location!");
moveCamera(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()),DEFAULT_zoom);
}else{
Log.d(TAG,"onComplete: current location is null");
Toast.makeText(CommuterHomeactivity.this,"Unable to get current location",Toast.LENGTH_SHORT).show();
}
}
});
}
}catch(SecurityException e){
Log.e(TAG,"getDeviceLocation: SecurityException: " + e.getMessage() );
}
}
private void moveCamera(LatLng latLng,float zoom){
Log.d(TAG,"moveCamera: Moving the camera to lat: " + latLng.latitude + ",long: " + latLng.longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngzoom(latLng,zoom));
}
private void initMap(){
Log.d(TAG,"initMap: Initializing Map");
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(CommuterHomeactivity.this);
}
//Check for Google Play Services Version via returning boolean
public boolean isGooglePlayVersionCompatible(){
Log.d(TAG,"isGooglePlayVersionCompatible: Checking Google Play Services Version");
int available = GoogleApiavailability.getInstance().isGooglePlayServicesAvailable(CommuterHomeactivity.this);
if(available == ConnectionResult.SUCCESS){
//Everything is fine and the user can make map requests
Log.d(TAG,"isGooglePlayVersionCompatible: Google Play Services is Working Properly...");
return true;
}else if(GoogleApiavailability.getInstance().isUserResolvableError(available)){
//An error occured but we can fix it
Log.d(TAG,"isGooglePlayVersionCompatible: An Error occured but we can fix it!");
Dialog dialog = GoogleApiavailability.getInstance().getErrorDialog(CommuterHomeactivity.this,available,ERROR_DIALOG_REQUEST);
dialog.show();
}else{
Toast.makeText(this,"You can not make Map Request!",Toast.LENGTH_SHORT).show();
}
return false;
}
//Get Location Permission
private void getLocationPermission(){
Log.d(TAG,"getLocationPermission: Getting Location Permissions");
String [] permissions = {Manifest.permission.accESS_FINE_LOCATION,Manifest.permission.accESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getapplicationContext(),FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
if(ContextCompat.checkSelfPermission(this.getapplicationContext(),COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
mLocationPermssionGranted = true;
initMap();
}else{
activityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
}
}else{
activityCompat.requestPermissions(this,LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
Log.d(TAG,"onRequestPermissionsResult: Called.");
mLocationPermssionGranted = false;
switch (requestCode)
{
case LOCATION_PERMISSION_REQUEST_CODE: {
if(grantResults.length > 0){
for(int i = 0; i < grantResults.length; i++){
if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
mLocationPermssionGranted = false;
Log.d(TAG,"onRequestPermissionsResult: Permission Failed!");
return;
}
}
Log.d(TAG,"onRequestPermissionsResult: Permission Granted.");
mLocationPermssionGranted = true;
//Initialize our map
initMap();
}
}
}
}