2

Hey guys I'm building an app and I need to get my longitude and latitude for some features I manage to get it but I can't pass it's values to my OnViewCreated method in my fragment. You can see those 2 simple toast messages in OnviewCreated they return null each time

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();

        };

This is my GPSTracker Activity

  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) {

}

}

  • make sure `onActivityResult` is getting called. – Bharatesh May 31 '16 at 12:41
  • @override innotation is not there in onactvityresult() method.then how come its called when it is called using setResult()?? – akhil Rao May 31 '16 at 12:42
  • @Spirit check [this](http://stackoverflow.com/questions/37202325/i-cannot-retrieve-my-current-longitude-and-latitude-in-google-maps/37203191#37203191) answer, works perfect for me. – Burak Cakir Jun 01 '16 at 07:36

1 Answers1

0

I manage to do it via following function

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);