2017-06-26 169 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地圖集成文檔中給出的應用中獲取地圖是一個非常簡單的一步一步的過程。

相關問題