2017-04-02 59 views
0

我不知道爲什麼我的地圖在我按下按鈕時沒有調用onLocationChanged。這是我的權限:onLocationChanged從來沒有叫

<uses-permission android:name="android.permission.ACCESS_GPS" /> 
<uses-permission android:name="android.permission.ACCESS_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

這是我的課:

公共類NuevaCarreraFragment擴展片段實現OnMapReadyCallback,LocationListener的,View.OnClickListener {

private View view; 
private LiveButton btnIniciarCarrera; 
private LiveButton btnFinalizarCarrera; 
private SupportMapFragment mapFragment; 
private GoogleMap mGoogleMap; 
private LocationManager locationManager; 


public static NuevaCarreraFragment newInstance() { 
    Bundle args = new Bundle(); 
    NuevaCarreraFragment fragment = new NuevaCarreraFragment(); 
    fragment.setArguments(args); 
    return fragment; 
} 

/** Ciclo de vida **/ 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

    // Comprobamos si la vista ya ha sido inflada previamente 
    if (view != null) { 
     ViewGroup parent = (ViewGroup) view.getParent(); 
     if (parent != null) 
      parent.removeView(view); 
    } 

    try {// Si no ha sido inflada previamente... 
     view = inflater.inflate(R.layout.fragment_nueva_carrera, container, false); 

     // Instanciamos los componentes 
     btnIniciarCarrera = (LiveButton) view.findViewById(R.id.button_iniciar_carrera); 
     btnFinalizarCarrera = (LiveButton) view.findViewById(R.id.button_finalizar_carrera); 
     mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 

     // Escuchamos a nuestros componentes 
     btnIniciarCarrera.setOnClickListener(this); 
     btnFinalizarCarrera.setOnClickListener(this); 

     // Sincronizamos nuestro mapa 
     mapFragment.onCreate(savedInstanceState); 
     mapFragment.getMapAsync(this); 

    } catch (InflateException e) { 
     e.printStackTrace(); 
    } 

    return view; 
} 

@Override 
public void onStart() { 
    super.onStart(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    if (mapFragment != null) { 
     mapFragment.onResume(); 
    } 
} 

@Override 
public void onPause() { 
    if (mapFragment != null) { 
     locationManager.removeUpdates(this); 
     mapFragment.onPause(); 
    } 
    super.onPause(); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
} 

@Override 
public void onDestroy() { 
    Fragment fragment = getFragmentManager().findFragmentById(R.id.map); 
    if (fragment != null) { 
     getFragmentManager().beginTransaction() 
       .remove(fragment) 
       .commit(); 
    } 
    super.onDestroy(); 
} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    Fragment fragment = getFragmentManager().findFragmentById(R.id.map); 
    if (fragment != null) { 
     getFragmentManager().beginTransaction() 
       .remove(fragment) 
       .commit(); 
    } 
} 


@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    if (mapFragment != null) { 
     mapFragment.onLowMemory(); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    if (mapFragment != null) { 
     mapFragment.onSaveInstanceState(outState); 
    } 
} 

/** OnClickListener **/ 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.button_iniciar_carrera: 
      // Inicializamos el localizador 
      locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
      if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
      if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
      } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      } 
      break; 
     case R.id.button_finalizar_carrera: 
      locationManager.removeUpdates(this); 
      break; 
    } 

} 

/** OnMapReadyCallback and LocationListener interface methods **/ 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 

    LatLng sydney = new LatLng(-33.867, 151.206); 

    mGoogleMap.addMarker(new MarkerOptions() 
      .title("Sydney") 
      .snippet("The most populous city in Australia.") 
      .position(sydney)); 

    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); 
} 

/** LocationListener interface methods **/ 

@Override 
public void onLocationChanged(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    LatLng misCoordenadas = new LatLng(latitude, longitude); 
    Geocoder geocoder = new Geocoder(getContext()); 
    try { 
     List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); 
     String direccion = addressList.get(0).getAddressLine(0); 
     direccion += "," + addressList.get(0).getLocality(); 
     direccion += "," + addressList.get(0).getCountryName(); 

     mGoogleMap.addMarker(new MarkerOptions() 
       .position(misCoordenadas) 
       .title(direccion) 
       .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); 
     mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(misCoordenadas, 17)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

正確顯示的地圖,但它沒有得到我的位置:(。 任何想法?

UPDATE

最後,我解決了實現Google API Client和Fused Location Api的問題。

回答

0
  1. 該應用是否有權限?你是否處理了未授予的案件?
  2. 而那些提供商的應用程序請求已啓用?
  3. 你調試了應用程序,並達到locationManager.requestLocationUpdates行嗎?

好像應用程序沒有請求因缺乏1個2

+0

權限和供應商的所有權利。但該應用永遠不會到達locationManager.requestLocationUpdates。 :S。我也意識到,如果我把這些方法放在onCreate中,地圖不會出現。任何想法 ? –