2016-08-07 37 views
0

Ich habe Google Map in Android-App integriert funktioniert alles gut, aber wenn die App erkennt, dass das GPS des Handys nicht aktiviert ist gibt es ein Dialogfeld zu GPS aktivieren und wenn der Benutzer dies tut und zur Anwendung zurückkehrt, reagiert die Karte nicht mehr und wird blau (kann sich im Ozean konzentrieren) und wenn ich das Gerät schüttele, wird die Karte aktiv und konzentriert sich auf den aktuellen Standort des Benutzers.Meine Google-Karte funktioniert nicht, nachdem Benutzer GPS aktiviert und die Anwendung

Ich möchte diese App im Hochformat ausführen, so dass die Shake-Option nicht sehr hilfreich sein wird.So kann mir jemand mit dem Code helfen und mir sagen, wo ich falsch liege. Ich habe versucht, verschiedene Links und Methoden in onResume, onRestart, aber nichts war hilfreich.

Hier ist mein Code Ich rufe diese setupmapifneeded Funktion in onCreate, onResume und in onRestart, aber nichts ist hilfreich.

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 

    if (mapFragment == null) { 
     // Try to obtain the map from the SupportMapFragment. 

     mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap googleMap) { 
       mMap = googleMap; 

       if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        return; 
       } 
       mMap.setMyLocationEnabled(true); 


       // creating GPS Class object 

       gps = new GPSTracker(MainActivity.this); 

       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15.0f)); 



       googleMap.addMarker(new MarkerOptions() 
         .position(new LatLng(26.89209, 75.82759)) 
         .title("FUN N FOOD")); 

       // check if GPS location can get 
       if (gps.canGetLocation()) { 
        Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude()); 
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15.0f)); 
       } else { 


        gpsDialog = new MaterialDialog.Builder(MainActivity.this) 
          .titleColor(getResources().getColor(R.color.grey)) 
          .title("GPS Settings") 
          .content("GPS is not enabled. Do you want to go to settings menu?") 
          .positiveText("Settings") 
          .negativeText("Cancel") 
          .positiveColorRes(R.color.grey) 
          .negativeColorRes(R.color.grey) 
          .onPositive(new MaterialDialog.SingleButtonCallback() { 
           @Override 
           public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
            gpsDialog.dismiss(); 
           } 
          }) 
          .onNegative(new MaterialDialog.SingleButtonCallback() { 
           @Override 
           public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { 
            gpsDialog.dismiss(); 
           } 
          }) 
          .show(); 
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 14.0f)); 

       } 
      } 
     }); 

    } 
} 

hier ist mein activity.main

<fragment 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

und hier ist meine andere Methoden

@Override 
public void onPause() { 

    if (mAdView != null) { 
     mAdView.pause(); 
    } 
    super.onPause(); 

} 

@Override 
public void onResume() { 


    super.onResume(); 

    if (mAdView != null) { 
     mAdView.resume(); 
    } 

    if (mapFragment == null) { 
     setUpMapIfNeeded(); 
    } 

} 

@Override 
protected void onRestart() { 
    super.onRestart(); 

    if (mapFragment == null) { 
     setUpMapIfNeeded(); 
    } 
} 

@Override 
public void onDestroy() { 
    if (mAdView != null) { 
     mAdView.destroy(); 
    } 
    super.onDestroy(); 
} 

Jeder kann dieser etwas ... Vielen Dank im Voraus

Antwort

0

helfen fand ich eine sehr einfache Möglichkeit, dies zu tun, indem Sie den onResume Teil zunächst ignoriert i this in Haupttätigkeit (Fused Standort Provider Api) integriert.

und dann in meinem Splash-Screen rief ich eine Methode, die mir geholfen, gps wie die OLA-App macht und wenn der Splash geht zur nächsten Aktivität beginnt es auf den aktuellen Standort des Benutzers ohne jedes Problem zu konzentrieren.

können wir tun, um die GPS-Sache so: Erstellen Sie eine Funktion

private GoogleApiClient mGoogleApiClient; 

private void automaticGps() { 


    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API).addConnectionCallbacks(Splash.this) 
       .addOnConnectionFailedListener(Splash.this).build(); 
     mGoogleApiClient.connect(); 
     LocationRequest locationRequest = LocationRequest.create(); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     locationRequest.setInterval(30 * 1000); 
     locationRequest.setFastestInterval(5 * 1000); 
     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(locationRequest); 

     // ************************** 
     builder.setAlwaysShow(true); // this is the key ingredient 
     // ************************** 

     PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi 
       .checkLocationSettings(mGoogleApiClient, builder.build()); 
     result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
      @Override 
      public void onResult(LocationSettingsResult result) { 
       final Status status = result.getStatus(); 
       final LocationSettingsStates state = result 
         .getLocationSettingsStates(); 
       switch (status.getStatusCode()) { 
        case LocationSettingsStatusCodes.SUCCESS: 
         // All location settings are satisfied. The client can 
         // initialize location 
         // requests here. 

         break; 
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
         // Location settings are not satisfied. But could be 
         // fixed by showing the user 
         // a dialog. 
         try { 
          // Show the dialog by calling 
          // startResolutionForResult(), 
          // and check the result in onActivityResult(). 
          status.startResolutionForResult(Splash.this, 1000); 
         } catch (IntentSender.SendIntentException e) { 
          // Ignore the error. 
         } 
         break; 
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
         // Location settings are not satisfied. However, we have 
         // no way to fix the 
         // settings so we won't show the dialog. 
         break; 
       } 
      } 
     }); 
    } 
} 

Überschreibung Methoden:

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    Intent i = new Intent(Splash.this,MainActivity.class); 
    startActivity(i); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

und implementieren:

public class Splash extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 

Aufruf dieser Funktion in onCreate

if(!gps.canGetLocation) { 
       automaticGps(); 
       //iv.startAnimation(an2); 
      } 
      else { 
       //iv.startAnimation(an2); 
       finish(); 
       Intent i = new Intent(Splash.this,MainActivity.class); 
       startActivity(i); 
      } 

Merke:

1) verwenden anstelle von android.Manifest.permission.ACCESS_FINE_LOCATION Manifest.permission.ACCESS_FINE_LOCATION

2) entfernen, diese Zeilen aus onPause():

if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    }