gpt4 book ai didi

当应用程序加载在 Fragment 中不起作用时,Android map 缩放到当前位置

转载 作者:行者123 更新时间:2023-11-29 14:46:04 25 4
gpt4 key购买 nike

我正在尝试编写一个代码,在应用程序加载时将 map 缩放到当前位置。

这是我用来缩放 map 的代码。

  //Zoom to the current location
public Location getMyLocation() {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}

return location;
}

然后我调用了Fragment的onCreateView里面的方法。这是代码。

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if(!isGooglePlayServiceAvailable())
{
return null;
}

if(rootView != null)
{
ViewGroup parent = (ViewGroup)rootView.getParent();
if(parent != null)
{
parent.removeView(rootView);
}
}
else
{
rootView = inflater.inflate(R.layout.google_maps, container, false);
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();


map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

String bestProvider = locationManager.getBestProvider(criteria, false);
if(bestProvider != null)
{
Location location = locationManager.getLastKnownLocation(bestProvider);
if(location != null)
{
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}

}

return rootView;
}

但是,我的代码似乎没有执行这个缩放部分。

我是 Android 的新手,这是我在 fragment 中使用的第一个代码(我之所以这样说是因为这对高级人员来说可能是一个低级问题)。那么,有人可以告诉我有没有具体的方法来实现 fragment ?

提前致谢。 :)

-编辑-

这是我的 GoogleMapsFragment.java

public class GoogleMapsFragment extends android.support.v4.app.Fragment implements LocationListener {

View rootView;
static final LatLng HAMBURG = new LatLng(53.558, 9.927);

private GoogleMap map;

public GoogleMapsFragment()
{
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if(!isGooglePlayServiceAvailable())
{
return null;
}

if(rootView != null)
{
ViewGroup parent = (ViewGroup)rootView.getParent();
if(parent != null)
{
parent.removeView(rootView);
}
}
else
{
rootView = inflater.inflate(R.layout.google_maps, container, false);
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();


map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

String bestProvider = locationManager.getBestProvider(criteria, false);
if(bestProvider != null)
{
Location location = locationManager.getLastKnownLocation(bestProvider);
if(location != null)
{
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}

}

return rootView;
}

//Zoom to the current location
public Location getMyLocation() {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}

return location;
}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

map.setMyLocationEnabled(true); // Identify the current location of the device

Location currentLocation = getMyLocation(); // Calling the getMyLocation method
}

@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(latLng));
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(15));
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

private boolean isGooglePlayServiceAvailable()
{
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
if(ConnectionResult.SUCCESS == status)
{
return true;
}
else
{
GooglePlayServicesUtil.getErrorDialog(status, getActivity(), 0).show();
return false;
}
}
}

最佳答案

1.实现接口(interface)

implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener

2.添加

 private GoogleApiClient mGoogleApiClient;  
private GoogleMap mMap;
private LatLng latlng;

3.onCreateView方法中添加这段代码

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);

if (mapFragment != null)
mapFragment.getMapAsync(this);

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

4. OnMapReady 方法

 @Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;


mMap.addMarker(new MarkerOptions().position(latlng).title("")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

// // Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 3000, null);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}

5. 覆盖方法onConnected

 @Override
public void onConnected(@Nullable Bundle bundle) {
if (ContextCompat.checkSelfPermission(mActivity,
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null)
latlng = new LatLng(location.getLatitude(),location.getLongitude());
return;
}
//call onMapReady override method
onMapReady(mMap);
}

6.

 @Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}

关于当应用程序加载在 Fragment 中不起作用时,Android map 缩放到当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429706/

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