MapsApp,计算两点之间的距离,Android
Posted
技术标签:
【中文标题】MapsApp,计算两点之间的距离,Android【英文标题】:MapsApp that calculates distance between 2 points, Android 【发布时间】:2016-04-28 08:38:44 【问题描述】:我想创建一个可以计算两个位置之间距离的应用程序,第一个是 GPS 查找,第二个是我设置的。应用打不开,错误在
第 61 行:距离 = location.distanceTo(locationSede);
帮帮我!
package com.example.prova.mapapplicationprove;
import android.Manifest;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity implements LocationListener
private float distance;
private float[] distfloat = new float[1];
private final LatLng POINT = new LatLng(0, 0); //longitudine e latitudine
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
LatLng sede = new LatLng(45.012447, 7.621013);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(POINT, 1));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
map.addMarker(new MarkerOptions().position(sede).title("Soluzioni"));
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
if(provider != null & !provider.equals(""))
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(provider, 2000, 1, this);
Location locationSede = new Location("Sede");
locationSede.setLatitude(45.012447);
locationSede.setLongitude(7.621013);
distance = location.distanceTo(locationSede); //LINE NUMBER 61
// location.distanceBetween(sede.latitude,sede.longitude,location.getLatitude(),location.getLongitude(),distfloat);
if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getApplicationContext(),"Location not found, GPS disabled.",Toast.LENGTH_LONG ).show();
else
Toast.makeText(getApplicationContext(),"Provider is null.",Toast.LENGTH_LONG).show();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
return;
@Override
public void onLocationChanged(Location location)
Toast.makeText(this,"Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude(),Toast.LENGTH_LONG ).show();
TextView textView2 = (TextView) findViewById(R.id.textview2);
TextView textView3 = (TextView) findViewById(R.id.textview3);
TextView textView4 = (TextView) findViewById(R.id.textview4);
String distanza = String.valueOf(distance);
//String distanza = String.valueOf(distfloat[0]);
textView2.setText("Latitude="+location.getLatitude());
textView3.setText("Longitude="+ location.getLongitude());
textView4.setText("Distance="+ distanza);
@Override
public void onStatusChanged(String s, int i, Bundle bundle)
@Override
public void onProviderEnabled(String s)
@Override
public void onProviderDisabled(String s)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prova.mapapplicationprove">
<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" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Per usare il GPS -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.prova.mapapplicationprove.MainActivity" />
</activity>
</application>
</manifest>
【问题讨论】:
请添加您的 logcat 以帮助我们了解问题 @SarahMaher 抱歉,logcat 是什么? 你有解决办法吗? @FrancescaBaka logcat 表示你的错误日志报告 @FrancescaBaka 请参阅下面的答案,您可以在距离之间获得两个位置。 【参考方案1】:我觉得你应该放
distance = location.distanceTo(locationSede); //LINE NUMBER 61
进入方法
@Override
public void onLocationChanged(Location location)
看起来像
@Override
public void onLocationChanged(Location location)
if (location != null)
Location locationSede = new Location("Sede");
locationSede.setLatitude(45.012447);
locationSede.setLongitude(7.621013);
int distance = location.distanceTo(locationSede);
【讨论】:
@FrancescaBaka:你能发布 logcat 来检查一下吗?【参考方案2】:如果你想计算两点之间的距离,那么你可以使用Location.distanceBetween( A.Latitude, A.Longitude, B.Latitude, B.Longitude, result);
查看下面的代码如何在你的代码中实现
float[] result = new float[1];
Location.distanceBetween(location.getLatitude(), location.getLongitude(), locationSede.getLatitude(), locationSede.getLongitude(), result);
Log.e("TAG Result",""+result); //Here result is your distance
double km = result * 0.001;
Log.e("TAG Result",""+km); //Here distance in kilometer
在这里,当您想以公里为单位时,总距离乘以 0.001,例如 distancein_km=total_dis*001
【讨论】:
我也用这种方式证明过,但还是有错误。 你能不能把错误日志发给你或者见我here【参考方案3】:我解决了我的问题,错误是由提供程序引起的:我使用 locationManager.allProvider();并以这种方式工作。 还是谢谢
【讨论】:
以上是关于MapsApp,计算两点之间的距离,Android的主要内容,如果未能解决你的问题,请参考以下文章