2017-06-26 127 views
0

我正在关注一个在线课程来创建优步克隆。但是,我有一个问题。从模拟器中,单击骑手选项时,我想重定向到一个名为“RiderActivity”的新活动,并且应该显示地图。然而,没有任何反应。Android:谷歌地图不显示在我的应用程序

这里是我的新的活动代码:

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); 
    } 
} 

回答

0

仿真器不支持谷歌地图,直到刚刚现在,您需要下载支持Play商店的新模拟器。 如果你已经完成了上述工作,你可能想要交叉检查你的谷歌地图API key,并检查你是否在开发者API控制台中启用了地图API。 在Google地图集成文档中给出的应用中获取地图是一个非常简单的一步一步的过程。

相关问题