是否可以获取用户位置并将其存储在变量中,以便将其用于其他目的,例如向用户提供从其位置到 XYZ 的方向,或更改用户位置的标记样式。
目前,我有这个;
map.setMyLocationEnabled(true);
我已经搜索了几个小时,但似乎找不到任何有用的信息来解释我如何存储用户当前位置。我正在使用 fragment 而不是 Activity 类。
很抱歉,如果这个问题听起来很愚蠢。
最佳答案
执行您所要求的操作的一种简单方法是检索用户的 LastKnownLocation
(如上面评论中的建议),然后创建您自己的 Location
类这样您就可以创建一个变量,在其中存储您认为与用户位置相关的所有信息(即纬度、经度、地址等)。
示例Location
类可能看起来像这样......
public class UserLocation {
private String name; // name of the location
private double latitude; // latitude (-90 to +90)
private double longitude; // longitude (-180 to +180)
private String address; // address of the location
... // constructor(s), getters, setters, other methods
}
将用户的当前位置存储在 Location
变量中可能如下所示...
...
// get last known location
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
// create variable to store the user's location
UserLocation currentLocation = new UserLocation();
// set values of our location variable
currentLocation.setLatitude(location.getLatitude());
currentLocation.setLongitude(location.getLongitude());
...
关于android - 获取用户位置并将其存储在 Android 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27770620/