Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么谷歌地图摄像头不在我的位置?_Android_Google Maps_Android Fragments - Fatal编程技术网

Android 为什么谷歌地图摄像头不在我的位置?

Android 为什么谷歌地图摄像头不在我的位置?,android,google-maps,android-fragments,Android,Google Maps,Android Fragments,我有android中谷歌地图的代码,我希望相机在我的位置,但相机在其他位置,我不知道为什么会发生这种情况 任何人都可以帮我找出这种情况发生的原因 这是我的片段代码: public class FragmentMap extends Fragment implements LocationListener { MapView mapView; GoogleMap map; double latitude; double longitude; public String bestProvider;

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

你的唯一改变应该是-

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

}
这将把相机移到您当前的位置


让我知道它是否对您有效。

地图现在在哪里,您是否调用了map.invalidate()?gps的位置正确,但相机错误最佳教程仍然不工作兄弟仍然错过相机位置:(我试着调试long and lat 1st Now是否调用了onlocationchange?我知道long and latitude是错误的如何获得正确的经度和纬度?嘿,伙计,你知道locationlistener是如何工作的吗?这会给你一个从gps卫星获取的位置。所以这有时可能会给出准确的位置和错误的位置。因为你可能会有信号问题。所以打开字段并检查它。它会给你准确的位置,谷歌地图摄像头会移动到由locationlistener提供的位置。所以这一切都取决于locationlistener。如果调用此方法并且你的谷歌地图正常,那么你就可以开始了。