获得经度和纬度

时间:2016-05-31 12:20:13

标签: java android google-maps android-fragments android-studio

嘿伙计我正在构建一个应用程序,我需要获得我的经度和纬度来获得一些功能,但是我无法将它的值传递给我的OnViewCreated方法分段。您可以在OnviewCreated中看到这两条简单的Toast消息,每次都返回null

LocatorFragment

expanded

这是我的GPSTracker活动

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

        };

}

1 个答案:

答案 0 :(得分:0)

我设法通过以下功能

来完成
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);