簡體   English   中英

為什么谷歌地圖相機不在我的位置?

[英]why the google maps camera not in my location?

我在片段 android 中有谷歌地圖的代碼,我想要相機在我的位置,但相機在其他位置,我不知道為什么會發生這種情況任何人都可以幫助我找出為什么會發生這種情況

這是我的片段代碼:

public class FragmentMap extends Fragment implements LocationListener {

MapView mapView;
GoogleMap map;
double latitude;
double longitude;
public String bestProvider;
public Criteria criteria;


@Override
public void onLocationChanged(Location location) {

    // Getting latitude of the current location
    latitude = location.getLatitude();

    // Getting longitude of the current location
    longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    map.animateCamera(CameraUpdateFactory.zoomTo(15));

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.map_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();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);


    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    MapsInitializer.initialize(this.getActivity());

    // Updates the location and zoom of the MapView
    map.setMyLocationEnabled(true);

    criteria = new Criteria();
    bestProvider = String.valueOf(MainActivity.locationManager.getBestProvider(criteria, true)).toString();
    Location location = MainActivity.locationManager.getLastKnownLocation(bestProvider);

    if (location != null) {
        onLocationChanged(location);
    }

    CameraUpdate cameraUpdate =
            CameraUpdateFactory
                    .newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16);
    map.animateCamera(cameraUpdate);

    return v;
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

}

這是我的位置聲明:

public static LocationManager locationManager;
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

你的 onlocationchange 應該是 -

@Override
public void onLocationChanged(Location location) {

CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(location.getLatitude(), location.getLongitude()))
                .zoom(14)
                .build();

if (map != null) {
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

}

這會將相機移動到您當前的位置。

讓我知道它是否對你有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM