我写了一个代码,它给了我经度和纬度的新值。我想将它们存储在ParseUser的对象用户中。用户已有两列纬度和经度。我试图像这样更新这些值。检查代码。我做对了吗?注意我使用GooglePlay服务进行位置更新
if(mRequestingLocationUpdates){
ParseUser user = ParseUser.getCurrentUser();
user.put("Latitude",lattitude);
user.put("Longitude",longitude);
user.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e != null){
}else
{
}
}
});
}
答案 0 :(得分:1)
最好的方法是实现LocationListener。
public class MainActivity extends Activity implements LocationListener
在OnCreate上创建所需的一切
mapView = (MapView) rootView.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mapView.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
然后使用" onLocationChanged"
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("My Location");
mp.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
googleMap.addMarker(mp);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
每次用户移动一点时,都会更新位置。它比每10秒检查一次并终止电池寿命更好。