谷歌地图没有在android

时间:2017-02-19 08:04:16

标签: java android google-maps location

我正在尝试创建一个Android应用程序,我想要获取当前位置并想要缩放它。当我在户外或在空中时,我可以获取我的位置。但是一段时间后,当我尝试在我的房间或室内调试应用程序时,它无法获取我当前的位置。我还是不明白是什么问题。任何帮助,将不胜感激。下面是我的代码。谢谢!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    tvDistanceDuration = (TextView)findViewById(R.id.textView5);

    super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

    // Makes Progress bar Visible
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

    //tvDistanceDuration = (TextView) findViewById(R.id.tv_distance_time);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;


    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    }
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setMapToolbarEnabled(true);

    // Determining Current Location

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());


    if (location != null) {
        double latitude = location.getLatitude();  // get current latitude
        double longitude = location.getLongitude();  // get current longitude
        myPosition = new LatLng(latitude, longitude);

        LatLng coordinate = new LatLng(latitude, longitude);
        CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13);
        mMap.animateCamera(yourLocation);
    }

}

0 个答案:

没有答案