作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从 Main 类获取位置或纬度经度,但如果我获取位置它返回 null,如果我获取纬度和经度则返回 0.0,如下面的代码所示。尽量减少代码并使其尽可能清晰。
这是我需要从主类获取位置的服务类
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
if(MainActivity.isMarkerDragged()) {
Location markerLocation = new Location(LocationManager.GPS_PROVIDER);
markerLocation.setLatitude(MainActivity.getMarkerLat());
markerLocation.setLongitude(MainActivity.getMarkerLon());
Toast.makeText(mContext, MainActivity.getMarkerLon() + " = " + MainActivity.getMarkerLat(), Toast.LENGTH_SHORT).show();
// Current location
distance = location.distanceTo(markerLocation);
Toast.makeText(mContext, "marker dragged " + distance, Toast.LENGTH_SHORT).show();
}
}
在 onLocationChanged 方法上,我得到了这个经度和纬度
endPoint.setTitle(endPoint.getPosition().longitude + ", " + endPoint.getPosition().latitude);
这按计划工作但是当我得到服务位置时它返回 0.0(糟透了)
public static Marker endPoint;
public static boolean markerDragged = true;
public static boolean serviceStarted = false;
private static double markerLat;
private static double markerLon;
/**
* Gets the current location of the device, and positions the map's camera.
*/
private void getDeviceLocation() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
/*
* Get the best and most recent location of the device, which may be null in rare
* cases when a location is not available.
*/
if (mLocationPermissionGranted) {
mLastKnownLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
}
// Set the map's camera position to the current location of the device.
if (mCameraPosition != null) {
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
} else if (mLastKnownLocation != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
// Adding a marker
endPoint = mMap.addMarker(new MarkerOptions()
.draggable(true)
.position(new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude())));
endPoint.setTitle(getString(R.string.drag));
} else {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
@Override
public void onMarkerDragEnd(Marker marker) {
if(!serviceStarted) {
startService(new Intent(this, LocationService.class));
}
}
@Override
public void onMarkerDragStart(Marker marker) {
setMarkerDragged(true);
markerLat = marker.getPosition().latitude;
markerLon = marker.getPosition().longitude;
endPoint.setTitle(endPoint.getPosition().longitude + ", " + endPoint.getPosition().latitude);
}
public static double getMarkerLon(){
return markerLon;
}
public static double getMarkerLat(){
return markerLat;
}
最佳答案
找到解决方案:在主类上:
Intent intent = new Intent(this, LocationService.class);
intent.putExtra("latitude", endPoint.getPosition().latitude);
intent.putExtra("longitude", endPoint.getPosition().longitude);
startService(intent);
和服务:
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
markerLat = (Double) intent.getExtras().get("latitude");
markerLon = (Double) intent.getExtras().get("longitude");
return START_STICKY;
}
关于java - 谷歌地图 GPS : longitude and latitude returns zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326187/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!