2016-05-31 14 views
2

Hey Leute, ich baue eine App und ich brauche meine Längen- und Breitengrade für einige Funktionen, die ich bekomme, aber ich kann ihre Werte nicht an meine OnViewCreated-Methode in meinem Fragment übergeben. Sie können diese 2 einfachen Toast Nachrichten in OnviewCreated sehen sie zurückkehren jede NullzeitLängen- und Breitengrad

LocatorFragment

public class LocatorFragment extends Fragment implements OnMapReadyCallback , GoogleApiClient.ConnectionCallbacks { 

private MapView mapView; 
private GoogleMap map; 
private GoogleApiClient mGoogleApiClient; 
private Double Latitude; 
private Double Longitude; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    Intent intent = new Intent(getContext(), GPSTrackerActivity.class); 
    startActivityForResult(intent,1); 
    View v = inflater.inflate(R.layout.locator_fragment, container, false); 


    // Gets the MapView from the XML layout and creates it 

    mapView = (MapView) v.findViewById(R.id.mapview); 
    mapView.onCreate(savedInstanceState); 

    // Gets to GoogleMap from the MapView and does initialization stuff 

    map = mapView.getMap(); 
    Location mLastLocation; 
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

    map.getUiSettings().setMyLocationButtonEnabled(false); 
    if (ActivityCompat.checkSelfPermission(getContext(), M anifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED  && ActivityCompat.checkSelfPermission(getContext(),  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 v; 



    } 
    map.setMyLocationEnabled(true); 

    // Needs to call MapsInitializer before doing any CameraUpdateFactory  calls 

    MapsInitializer.initialize(this.getActivity()); 

    // Updates the location and zoom of the MapView 


return v; 
} 

@Override 
public void onResume() { 
    mapView.onResume(); 
    super.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mapView.onPause(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Double longitude; 
    Double latitude; 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == 1){ 
     Bundle extras = data.getExtras(); 
     longitude = extras.getDouble("Longitude"); 
     latitude = extras.getDouble("Latitude"); 
     Latitude=latitude; 
     Longitude=longitude; 


    } 
} 
@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    Toast.makeText(getContext(),String.valueOf(Latitude),Toast.LENGTH_SHORT).show(); 
    Toast.makeText(getContext(),String.valueOf(Longitude),Toast.LENGTH_SHORT).show(); 

     }; 

Das ist mein GPSTracker Aktivität

public class GPSTrackerActivity extends AppCompatActivity implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 

private GoogleApiClient mGoogleApiClient; 
Location mLastLocation; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

} 

protected void onStart() { 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    try { 

     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      Intent intent = new Intent(); 
      intent.putExtra("Longitude", mLastLocation.getLongitude()); 
      intent.putExtra("Latitude", mLastLocation.getLatitude()); 
      setResult(1,intent); 
      finish(); 

     } 
    } catch (SecurityException e) { 

    } 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 

}

+0

stellen Sie sicher, dass 'onActivityResult' aufgerufen wird. – Bharatesh

+0

@override innotation ist nicht in onactivityresult() method.then wie kommt es aufgerufen, wenn es mit setResult() aufgerufen wird ?? –

+0

@Spirit überprüfen [this] (http://stackoverflow.com/questions/37202325/i-cannot-retrieve-my-current-longitude-and-latitude-in-google-maps/37203191#37203191) Antwort, funktioniert perfekt für mich. –

Antwort

0

schaffe ich es tun über folgende Funktion

map = mapView.getMap(); 
    map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 
     @Override 
     public void onMyLocationChange(Location location) { 
      Latitude= location.getLatitude(); 
      Longitude= location.getLongitude(); 
      Location locationzoom = map.getMyLocation(); 
      LatLng mapCenter = new LatLng(location.getLatitude(), location.getLongitude()); 
      CameraPosition cameraPosition = CameraPosition.builder() 
        .target(mapCenter) 
        .zoom(13) 
        .build(); 
      map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 
        2000, null); 
Verwandte Themen