Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取用户位置并将其存储在变量Android中_Android_Google Maps_Location - Fatal编程技术网

获取用户位置并将其存储在变量Android中

获取用户位置并将其存储在变量Android中,android,google-maps,location,Android,Google Maps,Location,是否可以获取用户位置并将其存储在变量中,以便用于其他目的,例如向用户提供从用户位置到XYZ的指示,或者更改用户位置的标记样式 现在我有这个, map.setMyLocationEnabled(true); 我已经搜索了几个小时,但似乎找不到任何有用的东西来解释如何存储用户当前位置。我正在使用片段而不是活动类 如果这个问题听起来很愚蠢,很抱歉。一个简单的方法就是检索用户的lastnownlocation(如上面的评论所建议的),然后创建一个自己的Location类,这样您就可以创建一个变量,在

是否可以获取用户位置并将其存储在变量中,以便用于其他目的,例如向用户提供从用户位置到XYZ的指示,或者更改用户位置的标记样式

现在我有这个,

map.setMyLocationEnabled(true); 
我已经搜索了几个小时,但似乎找不到任何有用的东西来解释如何存储用户当前位置。我正在使用片段而不是活动类


如果这个问题听起来很愚蠢,很抱歉。

一个简单的方法就是检索用户的
lastnownlocation
(如上面的评论所建议的),然后创建一个自己的
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
}
...
// 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());
...
将用户的当前位置存储在
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
}
...
// 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());
...

如果您使用GoogleMap,则可以通过GoogleMap中的OnMyLocationChangeListener轻松处理位置更改。 例如:

map.setOnMyLocationChangeListener(this);

//this - is some class which implements OnMyLocationChangeListener interface.

//method for handling this event.    
@Override
public void onMyLocationChange(Location location) {
    Logger.log(TAG, "My location changed!");
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    //TODO
}
public class MyLocations {
    private final List<LatLng> locations = new ArrayList<LatLng>();

    public void addLocation(LatLng latLng) {
        locations.add(latLng);
    }
    //other methods
}
所以,现在您可以处理用户位置的所有更改

我们还有一个用于存储单点的好类-LatLng。如果您需要存储点集,只需创建您自己的类,例如:

map.setOnMyLocationChangeListener(this);

//this - is some class which implements OnMyLocationChangeListener interface.

//method for handling this event.    
@Override
public void onMyLocationChange(Location location) {
    Logger.log(TAG, "My location changed!");
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    //TODO
}
public class MyLocations {
    private final List<LatLng> locations = new ArrayList<LatLng>();

    public void addLocation(LatLng latLng) {
        locations.add(latLng);
    }
    //other methods
}
此位置更改侦听器调用频率较高,可能会对电池造成不良影响,因此,如果要禁用它,只需在映射中将其设置为null:

map.setOnMyLocationChangeListener(null);

这应该有帮助,你能举个例子吗?