如何在android项目中单击按钮后获取当前位置?

时间:2012-05-12 16:14:43

标签: android android-maps

我是android的新手,我需要你的帮助。我有两个输入纬度和经度的编辑文本和一个按钮搜索。我的问题是如何在点击按钮搜索后在地图上显示位置?请在这里提供一些示例代码。

2 个答案:

答案 0 :(得分:0)

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
LocationManager locManager =
    (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if(locManager.getBestProvider(criteria,true) != null){
    Location myLocation= locManager.getLastKnownLocation(locManager.getBestProvider(criteria, true));
    String latitude = Double.toString(myLocation.getLatitude());
    String longitude = Double.toString(myLocation.getLongitude());
    String altitude = Double.toString(myLocation.getAltitude());
}

答案 1 :(得分:0)

根据我的理解,您希望将MapView设置为用户在EditText中放置的坐标。

float latitude = new Float(lat.getText().toString()); //Check for errors here
float longitude = new Float(long.getText().toString()); //Check for errors here

//Create a GeoPoint, which takes lat/long in microdegrees:
GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));

//Use MapController to move your mapview
MapController controller = yourMapView.getController();
controller.animateTo(point);
//Or, without the scroll animation:
//controller.setCenter(point);

您可以在此处阅读有关MapView和MapController类的更多信息: https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapController https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView