我试图计算谷歌地图上两个位置之间的距离。我从EditText
输入纬度和经度,
但返回值为零米。代码出了什么问题,即如何获得真正的距离?
这是我的应用的图片:
public class Ma`inActivity extends AppCompatActivity implements OnMapReadyCallback {
private TextView source;
private TextView destination;
private EditText sLatitude1;
private EditText sLongtiude1;
private EditText dLatitude2;
private EditText dLongtiude2;
private Button button;
private GoogleMap mMap;
boolean mapReady = false;
MarkerOptions elsedaway;
MarkerOptions Elrob3;
Location location;
static final CameraPosition elfayoum = CameraPosition.builder()
.target(new LatLng(29.309324, 30.842973))
.zoom(1)
.bearing(6)
.tilt(45)
.build();
double lati1;
double longi1;
double lati2;
double longi2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find text that display distance
textView = (TextView) findViewById(R.id.distance);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// find edit text and text view
source = (TextView) findViewById(R.id.sourc);
destination = (TextView) findViewById(R.id.destination);
sLatitude1 = (EditText) findViewById(R.id.lat1);
sLongtiude1 = (EditText) findViewById(R.id.long1);
dLatitude2 = (EditText) findViewById(R.id.lat2);
dLongtiude2 = (EditText) findViewById(R.id.long2);
// find button
button = (Button) findViewById(R.id.getDistance);
// find string from edittext
String lat1 = sLatitude1.getText().toString();
// parse string to double
lati1 = ParseDouble(lat1);
String lon1 = sLongtiude1.getText().toString();
longi1 = ParseDouble(lon1);
String lat2 = dLatitude2.getText().toString();
lati2 = ParseDouble(lat2);
String lon2 = dLongtiude2.getText().toString();
longi2 = ParseDouble(lon2);
Log.i("**lat", lat2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double xy1 = distanceBetween(new LatLng(lati1, longi1), new LatLng(lati1, longi2));
String distanceis = fmt(xy1) + "meter";
textView.setText(distanceis);
}
});
}
// mehtod to parse double from string
double ParseDouble(String strNumber) {
if (strNumber != null && strNumber.length() > 0) {
try {
return Double.parseDouble(strNumber);
} catch (Exception e) {
return -1; // or some value to mark this field is wrong. or make a function validates field first ...
}
} else return 0;
}
// get distance
public static Double distanceBetween(LatLng point1, LatLng point2) {
if (point1 == null || point2 == null) {
return null;
}
double vw = SphericalUtil.computeDistanceBetween(point1, point2);
Log.i("distance isby utillib ", String.valueOf(vw));
return vw;
}
public String fmt(double d) {
return String.format("%s", d);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mapReady = true;
mMap = googleMap;
// if(elsedaway!=null){
// mMap.addMarker(elsedaway);};
// mMap.addMarker(Elrob3);
mMap.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(lati1, longi1))
.add(new LatLng(lati2, lati2))
mMap.addCircle(new CircleOptions()
.center(new LatLng(29.291540, 30.601884))
.radius(500044)
.strokeColor(Color.GREEN)
.fillColor(Color.argb(54, 99, 255, 0)));
flyTo(elfayoum);
}
答案 0 :(得分:3)
答案 1 :(得分:2)
将这段代码移动到onCLick中,并更正您的值,您的代码应如下:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="body">
<div ng-controller="FrameController as vm">
<div id="myContainer">
<table id="table1">
<tbody>
<tr>
<td class="column1" style="width: 80px">Red Apple</td>
<td class="column2"> Lorem ipsum dolor sit amet </td>
<td class="column3">U$ 0.12</td>
</tr>
<tr>
<td class="column1">Red Apple</td>
<td class="column2"> Lorem ipsum dolor sit amet, </td>
<td class="column3">U$ 0.12</td>
</tr>
</tbody>
</table>
<table id="table2">
<tbody>
<tr>
<td class="column1">Red Apple</td>
<td class="column2"></td>
<td class="column3">U$ 0.12</td>
</tr>
<tr>
<td class="column1">Red Apple</td>
<td class="column2"></td>
<td class="column3">U$ 0.12</td>
</tr>
</tbody>
</table>
<table id="table3">
<tbody>
<tr>
<td class="column1">Red Apple</td>
<td class="column2"> Lorem ipsum dolor sit amet </td>
<td class="column3">U$ 0.12</td>
</tr>
<tr>
<td class="column1">Red Apple</td>
<td class="column2"> Lorem ipsum dolor sit amet, </td>
<td class="column3">U$ 0.12</td>
</tr>
</tbody>
</table>
</div>
<button ng-click="vm.pressTab()"> Press Tab</button>
</div>
</div>
答案 2 :(得分:1)
首先,这里有一个错误:
double xy1 = distanceBetween(new LatLng(lati1, longi1), new LatLng(lati1, longi2));
您应该在第二个参数中使用lati2
而不是lati1
。
另外,将getText()
移到onClick(...)
区块内。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
// find string from edittext
String lat1 = sLatitude1.getText().toString();
// parse string to double
lati1 = ParseDouble(lat1);
String lon1 = sLongtiude1.getText().toString();
longi1 = ParseDouble(lon1);
String lat2 = dLatitude2.getText().toString();
lati2 = ParseDouble(lat2);
String lon2 = dLongtiude2.getText().toString();
longi2 = ParseDouble(lon2);
...
}
});