我试图在应用程序加载时编写一个将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;
}
然后我在片段的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的新手,这是我在片段中使用的第一个代码(我告诉他们这是因为这对于高级人员来说可能是一个低级别的问题)。那么,有人可以告诉我有没有具体的方法来实现片段?
提前致谢。 :)
-edit -
这是我的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;
}
}
}
答案 0 :(得分:6)
1。实施界面
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. Override
方法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();
}
}