【发布时间】:2018-08-18 02:42:58
【问题描述】:
如果他让圈子,我正在尝试从用户那里获取范围,但我无法在屏幕上获取范围或距离,它总是显示 0.0,并且不显示 toast。
public class HomeActivity extends AppCompatActivity implements OnMapReadyCallback {
/**
* Request code for location permission request.
*
* @see #onRequestPermissionsResult(int, String[], int[])
*/
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
/**
* Flag indicating whether a requested permission has been denied after
returning in
* {@link #onRequestPermissionsResult(int, String[], int[])}.
*/
private boolean mPermissionDenied = false;
LatLng latLng;
GoogleMap mMap;
SupportMapFragment supportMapFragment;
Marker marker;
//textview para mostrar el email, nombre, id del usuario conectado actualmente
TextView textViewEmail;
Context context;
double latitudeF, longitudeF;
TextView locationText;
TextView tiempoText;
TextView rangoText;
TextView longitudeText;
TextView latitudeText;
//Float distance;
Circle circle;
String dist;
private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
supportMapFragment = (SupportMapFragment) getSupportFragmentManager
().findFragmentById(R.id.mapHomeUbication);
supportMapFragment.getMapAsync(this);
context = this;
// inicializamos el textview
textViewEmail = (TextView)findViewById(R.id.txt_EmailUsuario);
locationText = (TextView) findViewById(R.id.location);
longitudeText = (TextView) findViewById(R.id.longitude);
latitudeText = (TextView) findViewById(R.id.latitude);
tiempoText = (TextView) findViewById(R.id.tiempo);
rangoText = (TextView )findViewById(R.id.rango);
// cargamos el email desde sharedpreferences
SharedPreferences sharedPreferences = getSharedPreferences
(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "No Disponible");
// mostramos el email actualmente logueado
textViewEmail.setText("Usuario : " + email);
onMapReady(mMap);
}
// function salir
private void logout() {
//Creating an alert dialog to confirm logout
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Esta seguro que quiere salir?");
alertDialogBuilder.setPositiveButton("Sí",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//Getting out sharedpreferences
SharedPreferences preferences = getSharedPreferences
(Config.SHARED_PREF_NAME,Context.MODE_PRIVATE);
//Getting editor
SharedPreferences.Editor editor = preferences.edit();
//Puting the value false for loggedin
editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, false);
//Putting blank value to email
editor.putString(Config.EMAIL_SHARED_PREF, "");
//Saving the sharedpreferences
editor.commit();
//Starting login activity
Intent intent = new Intent(HomeActivity.this,
LoginActivity.class);
startActivity(intent);
}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
//Showing the alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Adding our menu to toolbar
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.log_OutMenu) {
//calling logout method when the logout button is clicked
logout();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
/**
* This is where we can add markers or lines, add listeners or move the camera.
In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once.
*/
private void setUpMap() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Permission to access the location is missing.
PermissionUtils.requestPermission(this,
LOCATION_PERMISSION_REQUEST_CODE,
Manifest.permission.ACCESS_FINE_LOCATION, true);
} else if (mMap != null) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(myLocationChangeListener());
}
}
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
return new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng latLngI = new LatLng(location.getLatitude(),
location.getLongitude());
longitudeF = location.getLongitude();
latitudeF = location.getLatitude();
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date(location.getTime());
String formatted = format.format(date);
tiempoText.setText(formatted.toString());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng
(latitudeF, longitudeF), 13));
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(new LatLng(latitudeF, longitudeF));
latLng = new LatLng(latitudeF, longitudeF);
drawMarkerWithCircle(latLng);
// Get back the mutable Circle
//circle = mMap.addCircle(circleOptions);
//getDistance(latLngI, latLng);
float[] distance = new float[2];
Location.distanceBetween( marker.getPosition().latitude,
marker.getPosition().longitude,
circle.getCenter().latitude, circle.getCenter
().longitude, distance);
if (distance[0] > circle.getRadius()) {
Toast.makeText(getBaseContext(), "fuera del rango: "
+ distance[0] + " radio: " + circle.getRadius(), Toast.LENGTH_LONG).show();
} else if (distance[0] < circle.getRadius()) {
Toast.makeText(getBaseContext(), "dentro del rango: "
+ distance[0] + " radio: " + circle.getRadius() , Toast.LENGTH_LONG).show();
}
//mMap.addMarker(new MarkerOptions().position(latLng));
//mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,8.0f));
locationText.setText("estas en [" + longitudeF + " ; " + latitudeF
+ "] ");
Log.d(TAG, "onMyLocationChange: 1" + distance);
}
};
}
private void drawMarkerWithCircle(LatLng position) {
double radiusInMeters = 300;
int strokeColor = 0xffff0000; //red outline
int shadeColor = 0x44ff0000; //opaque red fill
CircleOptions circleOptions = new CircleOptions().center(position).radius
(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
circle = mMap.addCircle(circleOptions);
MarkerOptions markerOptions = new MarkerOptions().position(position);
marker = mMap.addMarker(markerOptions);
}
}
这里是 logcat,它只显示一个数组,但如果移动标记,它看起来总是在变化
01-04 04:03:31.766 5838-5864/com.google.android.gms.persistent I/dalvikvm:找不到方法 android.location.Location.isFromMockProvider,引用自方法 com.google.android.location .util.am.e 01-04 04:03:31.766 5838-5864/com.google.android.gms.persistent W/dalvikvm: VFY: 无法解析虚拟方法 995: Landroid/location/Location;.isFromMockProvider ()Z 01-04 04:03:31.766 5838-5864/com.google.android.gms.persistent D/dalvikvm: VFY: 在 0x0019 处替换操作码 0x6e 01-04 04:03:31.822 9501-9501/com.jhon.co.divi D/MyApp: onMyLocationChange: 1[F@53a75360 01-04 04:03:31.834 5838-5838/com.google.android.gms.persistent I/dalvikvm: 找不到方法 android.os.Looper.quitSafely,引用自方法 com.google.android.gms.fitness。 service.b.c.onDestroy 01-04 04:03:31.842 5838-5838/com.google.android.gms.persistent W/dalvikvm: VFY: 无法解析虚拟方法 1351: Landroid/os/Looper;.quitSafely ()V 01-04 04:03:31.842 5838-5838/com.google.android.gms.persistent D/dalvikvm: VFY: 在 0x000f 处替换操作码 0x6e 01-04 04:03:31.854 5838-5838/com.google.android.gms.persistent V/GeofencerHelper:初始化地理围栏的系统缓存。 01-04 04:03:31.862 5838-5858/com.google.android.gms.persistent D/GeofenceStateCache:恢复 0 个地理围栏。 01-04 04:03:31.886 5838-5838/com.google.android.gms.persistent I/dalvikvm: 找不到方法 android.os.Process.myUserHandle,引用自方法 com.google.android.location.internal。服务器.o.f 01-04 04:03:31.886 5838-5838/com.google.android.gms.persistent W/dalvikvm: VFY: 无法解析静态方法 1464: Landroid/os/Process;.myUserHandle ()Landroid/os/UserHandle; 01-04 04:03:31.886 5838-5838/com.google.android.gms.persistent D/dalvikvm: VFY: 在 0x00a7 处替换操作码 0x71
看起来是这样的:
我不明白为什么它总是返回 0.0 并且没有在 Location.distanceBetween 中进行验证。
【问题讨论】:
-
您是否尝试记录标记和圆圈返回的纬度和经度值?,我觉得它们具有相同的值
-
您是对的,它们具有相同的值,我该如何解决?
标签: android google-maps