的最短距离的地方,我有我的手,几个地方的纬度和经度值在我的服务器。这些纬度和经度值从服务器发送并在android手机上成功接收。现在我需要让我的Android手机的当前位置,并计算最近的地方,我的当前位置,并标记它的谷歌地图上。我知道如何庆祝谷歌地图上的地方,但我不知道如何计算使用经度和纬度,从我目前的位置,其他位置的距离,找到我的位置的最短距离的地方。此外,我不知道如何使用编码从android设备获取我的当前位置。我听说的位置可以利用最后的已知位置两种方式,如1)被发现了,2)利用onlocation改变。使用上次已知的位置方法的当前位置不适合我的应用程序。所以我需要使用onlocation改变我的当前经度和纬度。如果它不可用,则从经度和纬度获取位置坐标。计算从我的当前位置的最短距离的地方和标记使用的标记在谷歌地图中的Android
这是我已经在我的应用程序打开谷歌地图和标记使用纬度和经度的地方的代码。
public class Map extends Activity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
double latitude =9.887262 ;
double longitude = 76.731675;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是它的布局文件..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
如何使用onlocation获取当前位置已更改。? – njnjnj 2014-09-20 04:59:17
在onLocation中改变了方法,你可以得到'location.getLatitude()'和'location.getLongitude()'。 – Piyush 2014-09-20 05:12:23
使用locationClient获取当前位置。尝试这样的: [http://stackoverflow.com/questions/16731717/using-locationclient-to-get-location-updates] – Supriya 2014-09-20 06:19:56