Android Studio Google Maps 可在 Android 设备上运行,但不能在模拟器上运行
Posted
技术标签:
【中文标题】Android Studio Google Maps 可在 Android 设备上运行,但不能在模拟器上运行【英文标题】:Android Studio Google Maps Working on Android Device but not on Emulator 【发布时间】:2021-08-07 01:03:33 【问题描述】:我正在尝试使用从 Google Cloud 平台获得的 Google Maps API 密钥来获取我在 android Studio 中的当前位置。 如果我在手机上运行该应用程序,一切正常。 如果我在模拟器上运行应用程序,我的位置似乎是 Google Plex,如下图所示(这将是完全错误的): emulator app showing the wrong location
我不确定这是因为 API 密钥还是代码/权限/GPS 中的错误? 我从教程中获取了代码,但现在似乎不推荐使用 Google 客户端(不确定这是否是问题所在)。 我相信是因为模拟器,但我不知道为什么。我目前使用的是 Pixel 2 API 26。
我将在这里显示代码:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
BUILD.GRADLE:
implementation 'com.google.android.gms:play-services-maps:17.0.1'
implementation 'com.google.android.gms:play-services-location:18.0.0'
地图活动:
public class GoogleMapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
private GoogleMap mMap;
private GoogleApiClient googleClient;
private LocationRequest locationRequest;
private Location ultimateLocation;
private Marker markerCurrentLocation;
private static final int Request_User_Location_Code = 90;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
checkLocationPermission();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
assert mapFragment != null;
mapFragment.getMapAsync(this);
@Override
public void onMapReady(@NotNull GoogleMap googleMap)
mMap = googleMap;
//current location
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
createGoogleApiClient();
mMap.setMyLocationEnabled(true);
protected synchronized void createGoogleApiClient() // ok
googleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleClient.connect();
@Override
public void onLocationChanged(@NonNull Location location) // ok
ultimateLocation = location;
if (markerCurrentLocation !=null)
markerCurrentLocation.remove();;
LatLng coordinates = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(coordinates);
markerOptions.title("Current Location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
markerCurrentLocation = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(coordinates));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 17.0f));
if(googleClient!=null)
LocationServices.FusedLocationApi.removeLocationUpdates(googleClient,this);
else
Toast.makeText(this, "NULLLLLLLL", Toast.LENGTH_SHORT).show();
@Override
public void onConnected(@Nullable Bundle bundle) // ok
locationRequest = new LocationRequest();
locationRequest.setInterval(1100);
locationRequest.setFastestInterval(1100);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
LocationServices.FusedLocationApi.requestLocationUpdates(googleClient, locationRequest, this);
public boolean checkLocationPermission() // ok
boolean ret;
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION))
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, Request_User_Location_Code);
else
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, Request_User_Location_Code);
ret = false;
else
ret = true;
return ret;
//ok
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
switch (requestCode)
case Request_User_Location_Code:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
if(googleClient==null)
createGoogleApiClient();;
mMap.setMyLocationEnabled(true);
else
Toast.makeText(this, " PERMISSION DENIED", Toast.LENGTH_SHORT).show();
return ;
@Override
public void onConnectionSuspended(int i)
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
【问题讨论】:
【参考方案1】:只需转到模拟器控件(三点菜单)并在Location
选项卡中,固定地图中的任意位置,然后单击Set Location
。
然后验证您是否在应用中收到了预期的位置。
【讨论】:
我正在尝试通过实时更新获取我的当前位置。我有一个代码,它适用于 android 设备,但不适用于模拟器,我的问题是它是否是模拟器的问题。可能模拟器没有GPS功能什么的? 模拟器没有 GPS 硬件,但是它具有所有的 API 和功能,并且会返回您想要的任何位置。虽然它不会返回您当前的位置,但您可以通过设置设置位置,然后在您的应用中验证它们。以上是关于Android Studio Google Maps 可在 Android 设备上运行,但不能在模拟器上运行的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio - Cannot reload AVD list (d:skin)
com.google.android.maps 在 Android Studio 中不存在
无法按照 Google“入门”页面中的说明在 Android Studio 中添加地图; Android Studio 报错