Я хочу знать расстояние между двумя координатами.
Когда координаты меняются, я хочу знать расстояние между ними в метре.
Код MainActivity:
private TextView textView;
private TextView label3,label2;
private EditText editText;
protected LocationManager locationManager;
private String name;
double latitude;
double longitude;
RequestQueue queue;
double Lan;
double Lon;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
label3 = (TextView)findViewById(R.id.label3);
label2 = (TextView)findViewById(R.id.label2);
textView = (TextView) findViewById(R.id.label);
int permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionStatus == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),"access is",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
1);
}
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10,
10, this);
queue = Volley.newRequestQueue(this);
}
@Override
public void onLocationChanged(final Location location) {
textView.setText("My location = Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
if (latitude != location.getLatitude() && longitude != location.getLongitude()) {
Toast.makeText(this,"location changed",Toast.LENGTH_LONG).show();
latitude = location.getLatitude();
longitude = location.getLongitude();
getvalue();
}
}
@Override
public void onProviderDisabled(String provider) {
Log.e("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.e("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Latitude","status");
}
public void Insert(View view){
name = editText.getText().toString();
Insert.BackGround b=new Insert.BackGround();
String lan = String.valueOf(latitude);
String lon = String.valueOf(longitude);
b.execute(name,lan,lon);
String result = b.Result();
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
public void getvalue(){
Log.e("st","getvalue");
String url = "http://my_url";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String name = response.getString("name");
Lan = response.getDouble("latitude");
Lon = response.getDouble("longitude");
Log.e("lan", String.valueOf(Lan));
Log.e("lon", String.valueOf(Lon));
calculate(latitude,longitude,Lan,Lon);
label3.setText("name="+name+" Lan="+Lan+" Lon="+Lon);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(request);
}
public void calculate( double lan1, double lon1, double lan2, double lon2){
Log.e("st","calculate");
Location loc1 = new Location("point A");
loc1.setLatitude(lan1);
loc1.setLongitude(lon1);
Location loc2 = new Location("point B");
loc2.setLatitude(lan2);
loc2.setLongitude(lon2);
String distanceInMeters = String.valueOf(loc1.distanceTo(loc2));
label2.setText("Distance = "+distanceInMeters );
}
Манифест:
<uses-sdk
android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
android:testOnly="false"
android:debuggable="true"
tools:ignore="HardcodedDebugMode">
Программа хорошо работает в Redmi Note 4 и эмуляторе BlueStacks, но в приложениях Redmi Note 5 и Android> 8 выдает такую ошибку «приложение продолжает останавливаться»
Не могу найти ошибку и ответ в интернете помогите мне пожалуйста
Я был бы очень рад, если бы кто-то написал код, который работает на любом устройстве:)
Android logcat
2020-02-26 18:22:42.553 3897-3897/com.example.getlocation E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
2020-02-26 18:22:42.554 3897-3897/com.example.getlocation E/GMPM: Scheduler not set. Not logging error/warn.
2020-02-26 18:22:42.560 3897-3927/com.example.getlocation E/GMPM: Uploading is not possible. App measurement disabled
2020-02-26 18:22:44.854 3897-3897/com.example.getlocation E/st: getvalue
2020-02-26 18:22:44.874 3897-3931/com.example.getlocation E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.getlocation, PID: 3897
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/app/com.example.getlocation-9UATZ0srGfq2w1rY4vtYKQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.getlocation-9UATZ0srGfq2w1rY4vtYKQ==/lib/arm64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)