Android:Google地图未显示在我的应用

时间:2017-06-26 07:33:08

标签: android google-maps android-intent

我正在关注创建Uber克隆的在线课程。但是,我遇到了问题。从模拟器中,当单击rider选项时,我想重定向到名为" 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 个答案:

答案 0 :(得分:0)

模拟器不支持谷歌地图,直到现在,您需要下载具有Play商店支持的新模拟器。 如果您已完成上述操作,您可能需要交叉检查您的google map api密钥并检查您是否在开发人员api控制台中启用了地图api。 在应用程序中获取地图是一个非常简单的一步一步的过程,如谷歌地图集成文档中所述。