gpt4 book ai didi

java - 获取经度和纬度

转载 作者:行者123 更新时间:2023-12-03 18:08:55 25 4
gpt4 key购买 nike

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

定位器 fragment

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

};

这是我的 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) {

}

}

最佳答案

我设法通过以下功能做到这一点

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

关于java - 获取经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com