onLocationChanged 指向错误的位置(巴伦支海)?
onLocationChanged pointing to wrong Location(Barents Sea)?
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
在模拟器和真实设备上测试。
那些坐标 75.8467264/30.862686
和 75.8467229/30.862678
在巴伦支海。
改变
LatLng latLng = new LatLng(location.getLongitude(), location.getLatitude());
到
LatLng latLng = new LatLng(location.getLatitude, location.getLongitude()());
在 onLocationChanged()
中查看地图上的印度北部。
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
在模拟器和真实设备上测试。
那些坐标 75.8467264/30.862686
和 75.8467229/30.862678
在巴伦支海。
改变
LatLng latLng = new LatLng(location.getLongitude(), location.getLatitude());
到
LatLng latLng = new LatLng(location.getLatitude, location.getLongitude()());
在 onLocationChanged()
中查看地图上的印度北部。