onLocationChanged指向错误的位置(巴伦支海)?

时间:2017-03-25 08:49:32

标签: android google-maps android-location

build.grade

  compile 'com.android.support:appcompat-v7:25.3.0'
  compile 'com.android.support.constraint:constraint-layout:1.0.1'
  compile 'com.google.android.gms:play-services:10.2.1'

的AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<meta-data android:name="com.google.android.geo.API_KEY"
        android:value="@string/str_map_key"/>

<meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements OnMapReadyCallback,
    ActivityCompat.OnRequestPermissionsResultCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private GoogleMap mGoogleMap;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private boolean mPermissionDenied = false;

private GoogleApiClient mGoogleApiClient;

private Location mLastLocation;

//TextView
TextView longitudeTextView;
TextView lattitudeTextView;

private LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_view);
    mapFragment.getMapAsync(this);

    longitudeTextView = (TextView) findViewById(R.id.main_longitude_text);
    lattitudeTextView = (TextView) findViewById(R.id.main_lattitude_text);
}

@Override
protected void onStart() {
    //when activity is create GoogleMapApiClient is build there under onCreate
    //than on activity start we gonna connect it to Google
    mGoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    //when activity is stopped disconnect GoogleApiClient
    super.onStop();
    mGoogleApiClient.disconnect();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

    enableMyLocation();
}

private void enableMyLocation() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        //Permission to access the location is missing
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                LOCATION_PERMISSION_REQUEST_CODE);
    } else {
        //Access to location has been granted to app
        mGoogleMap.setMyLocationEnabled(true);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
        return;
    }

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        // Enable the my location layer if the permission has been granted.
        enableMyLocation();
    } else {
        // Display the missing permission error dialog when the fragments resume.
        mPermissionDenied = true;
    }

}

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(100);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "Location permission missing !!", Toast.LENGTH_LONG).show();
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    //displaying coordinates on screen for test purpose only
    longitudeTextView.setText(String.valueOf(location.getLongitude()));
    lattitudeTextView.setText(String.valueOf(location.getLatitude()));

    LatLng latLng = new LatLng(location.getLongitude(), location.getLatitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
    mGoogleMap.moveCamera(cameraUpdate);
}
}

将相机移动到新坐标的非常基本的应用程序。

无论何时更改位置,都会调用

onLocationChanged。它应该将相机移动到新的坐标,但不是这样,它开始指向巴伦支海的某个地方。我已经把整个文档丢了5次以上仍然没有得到它。

在应用开始时 https://s10.postimg.org/xywc61w2x/Screenshot_20170325-123853.png

我实际上在哪里 https://s10.postimg.org/d4ol1yr3d/Screenshot_20170325-132620.png

在仿真器和真实设备上测试。

1 个答案:

答案 0 :(得分:0)

那些坐标75.8467264/30.86268675.8467229/30.862678位于巴伦支海。

更改

LatLng latLng = new LatLng(location.getLongitude(), location.getLatitude());

LatLng latLng = new LatLng(location.getLatitude, location.getLongitude()());
onLocationChanged()

在地图上查看印度北部。