android百度地图开发V4.5最新版(3)---计算地图上俩点之间的距离
接着前面的继续,先来看一个图片:
一个是获取自己的地理位置信息,一个是获取固定点到自己的距离。
俩个需求,第一个需求我们已经完成。有需要的可以看我的文章ndroid百度地图开发V4.5最新版(2)---地理位置的获取
接着实现第二个需求即距离的计算。
废话不多说,直接撸代码。
1,建立xml文件。代码如下:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入俩个点的经纬度:"
android:layout_marginLeft="10dip"
android:textColor="@color/colorAccent"
android:textSize="22dip"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个点的经纬度:"
android:layout_marginLeft="10dip"
android:textColor="@color/colorAccent"
android:textSize="22dip"
/>
<EditText
android:id="@+id/et_erjing"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginTop="22dip"
android:text="39.951721"
/>
<EditText
android:id="@+id/et_erwei"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginTop="22dip"
android:text="116.288394"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二个点的经纬度:"
android:layout_marginLeft="10dip"
android:textColor="@color/colorAccent"
android:textSize="22dip"
/>
<EditText
android:id="@+id/et_yijing"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginTop="22dip"
android:text="39.951721"
/>
<EditText
android:id="@+id/et_yiwei"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginTop="22dip"
android:text="116.288414"
/>
<Button
android:id="@+id/bt_jisuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算经纬度距离"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/tv_jieguo"
android:layout_width="match_parent"
android:layout_height="50dip" />
2.获取俩个点:
LatLng gp1=new LatLng(Double.parseDouble(et_yijing.getText().toString()),Double.parseDouble(et_yiwei.getText().toString()));
LatLng gp2=new LatLng(Double.parseDouble(et_erjing.getText().toString()),Double.parseDouble(et_erwei.getText().toString()));
3:计算距离:
Double distance= DistanceUtil. getDistance(gp1, gp2);
tv_jieguo.setText(
distance
+"米");
效果图如下所示: