2016-04-24 7 views
0

Ich möchte den aktuellen Standort des Benutzers auf Google Map zeigen, aber ab sofort kann ich den Standort nicht zeigen. Ich habe ein Fragment, in dem ich den Code schreibe.Google Karte zeigt den aktuellen Standort in andorid nicht an?

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.fragment_home, container, false); 

     initGamp(rootView); 



     // Inflate the layout for this fragment 
     return rootView; 
    } 

initgmap Funktion

public void initGamp(View root) { 

     SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     // check if map is created successfully or not 


    } 


@Override 
    public void onMapReady(GoogleMap googleMap) { 

     mMap = googleMap; 

     // Add a marker in Sydney, Australia, 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)); 
     if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), 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; 
     } 
     Log.i("MAP","BEFORE LOCATION"); 
     mMap.setMyLocationEnabled(true); 
     mMap.getUiSettings().setMyLocationButtonEnabled(true); 
     Log.i("MAP", "AFTER LOCATION"); 
     LocationManager locationManager = (LocationManager) getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String bestProvider = locationManager.getBestProvider(criteria, true); 
     Location location = locationManager.getLastKnownLocation(bestProvider); 
     if (location != null) { 
      Toast.makeText(getActivity().getApplicationContext(),"not null",Toast.LENGTH_SHORT).show(); 
      onLocationChanged(location); 
     } 
     locationManager.requestLocationUpdates(bestProvider, 20, 0, this); 

     Log.i("MAP", "END LOCATION"); 



    } 

    @Override 
    public void onLocationChanged(Location location) { 

     Log.i("MAP","CHANGE LOCATION"); 
     Toast.makeText(getActivity().getApplicationContext(),"chnage",Toast.LENGTH_SHORT).show(); 
     mMap.clear(); 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     LatLng latLng = new LatLng(latitude, longitude); 
     mMap.addMarker(new MarkerOptions().position(latLng)); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 


    } 

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


    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

Alle Berechtigungen:

<!-- Required to show current 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.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 

Above Code i für das Zeigen Standort verwendet haben, aber ich working.I muss nicht auch Taste der aktuellen location button beim Tippen, dass ich immer noch nicht den aktuellen Standort erhalte.

+0

Sie die Erlaubnis in einem offenkundigen gegeben haben ?? –

+0

Ja, ich habe @VivekMishra angegeben – Techiee

+0

Es sieht so aus, als ob Sie die Location-Berechtigung zur Laufzeit nicht anfordern, siehe hier: http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and -locationmanager-to-show-aktueller-Standort-auf-androi/34582595 –

Antwort

0

Der Schlüssel hier ist zu warten, bis der Rückruf onMapReady() aufgetreten ist, bevor Sie Standortaktualisierungen anfordern. Sobald eine Instanz dieser Schnittstelle auf ein Objekt MapFragment oder gesetzt ist, wird die Methode ausgelöst, wenn die Map zur Verwendung bereit ist und eine Nicht-Null-Instanz von GoogleMap bereitstellt.

Hier ist der vollständige Aktivitätscode, der das tut, was Sie brauchen. Registrieren Sie einen Standort-Listener, wenn die Aktivität geladen wird und wenn das Ereignis "Location Location" (Standortänderung) auftritt. Hier

ist die Probe Aktivitätscode:

public class MainActivity extends AppCompatActivity implements 
OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,  GoogleApiClient.OnConnectionFailedListener, LocationListener { 

LocationRequest mLocationRequest; 
GoogleApiClient mGoogleApiClient; 

LatLng latLng; 
GoogleMap mGoogleMap; 
SupportMapFragment mFragment; 
Marker currLocationMarker; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

mFragment = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map); 
mFragment.getMapAsync(this); 
} 

@Override 
public void onMapReady(GoogleMap gMap) { 
mGoogleMap = gMap; 
mGoogleMap.setMyLocationEnabled(true); 

buildGoogleApiClient(); 

mGoogleApiClient.connect(); 

} 

protected synchronized void buildGoogleApiClient() { 
Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show(); 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
.addConnectionCallbacks(this) 
.addOnConnectionFailedListener(this) 
.addApi(LocationServices.API) 
.build(); 
} 

@Override 
public void onConnect 
+0

Ihr Code ist unvollständig – Techiee

+1

http://Stackoverflow.com/a/33741294/4409409 –

Verwandte Themen