Android:谷歌地图未显示在我的应用程序中
Posted
技术标签:
【中文标题】Android:谷歌地图未显示在我的应用程序中【英文标题】:Android: Google maps not displaying in my app 【发布时间】:2017-11-29 00:11:26 【问题描述】:我正在学习创建 Uber 克隆的在线课程。但是,我有一个问题。在模拟器中,当单击骑手选项时,我想重定向到一个名为“RiderActivity”的新活动,并且应该显示一张地图。但是,什么也没有发生。
这是我的新活动代码:
public class RiderActivity extends FragmentActivity implements OnMapReadyCallback
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1)
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
updateMap(lastKnownLocation);
public void updateMap(Location location)
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
mMap.clear();
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rider);
// 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);
/**
* 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;
//setup location manager and listerner
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE) ;
locationListener = new LocationListener()
@Override
public void onLocationChanged(Location location)
updateMap(location);
@Override
public void onStatusChanged(String s, int i, Bundle bundle)
@Override
public void onProviderEnabled(String s)
@Override
public void onProviderDisabled(String s)
;
if(Build.VERSION.SDK_INT < 23)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
else
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,new String[]Manifest.permission.ACCESS_FINE_LOCATION,1);
else
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
updateMap(lastKnownLocation);
// 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));
在 MainActivity 中,我有一个方法
public void redirectActivity()
if(ParseUser.getCurrentUser().get("riderOrDriver") == "rider")
Intent intent = new Intent(getApplicationContext(),RiderActivity.class);
【问题讨论】:
【参考方案1】:模拟器不支持谷歌地图,直到刚才,您需要下载支持Play商店的新模拟器。 如果您已完成上述操作,您可能需要交叉检查您的 google map api 密钥并检查您是否在开发人员 api 控制台中启用了 maps api。 在应用程序中获取地图是一个非常简单的分步过程,如谷歌地图集成文档中所述。
【讨论】:
以上是关于Android:谷歌地图未显示在我的应用程序中的主要内容,如果未能解决你的问题,请参考以下文章