我对android很新,我正在开展一个学校项目,我必须使用谷歌地图。此外,我必须跟踪用户位置,当位置更改时,我必须更新数据库中的用户位置。到目前为止,我设法集成了gmaps,现在我正在改变位置。我在我的类上实现了接口LocationListener,我也覆盖了onLocationChanged方法,这种方法不起作用。我错过了什么?谢谢!
public class MapFragment extends Fragment implements LocationListener, DialogManager.OnDialogListener {
private GoogleMap gMap;
private SupportMapFragment smf;
private View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_main, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
smf = (SupportMapFragment) fm.findFragmentById(R.id.mainContent);
if (smf == null) {
smf = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.mainContent, smf).commit();
}
}
@Override
public void onResume() {
super.onResume();
if(!LocationUtils.getInstance().isGPSON(this.getActivity())) {
DialogManager.buildAlertMessageNoGps(this.getActivity(), this);
} else {
if(smf!=null) {
gMap = smf.getMap();
if(gMap != null) {
setUpMap();
}
}
}
}
private void setUpMap() {
Location location = LocationUtils.getInstance().getLastKnownLocation(this.getActivity());
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
gMap.setMyLocationEnabled(true);
gMap.animateCamera(CameraUpdateFactory.zoomTo(20));
gMap.addMarker(new MarkerOptions().position(latLng).title("I am here!")); //this works, but it put the market on the last known location, not where is my current gps location ..
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
List <Address> adr = LocationUtils.getInstance().getAddress(latLng, this.getActivity());
String str = adr.get(0).getAddressLine(0);
String city = adr.get(0).getAddressLine(1);
String coutry= adr.get(0).getAddressLine(2);
Toast.makeText(getActivity().getApplicationContext(),"str: "+ str+"\ncity: "+city+"\ncountry: "+country, Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getActivity().getApplicationContext(),"sssCHANGEDsss", Toast.LENGTH_LONG).show(); //this never appear
if(gMap != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
gMap.animateCamera(CameraUpdateFactory.zoomTo(20));
gMap.addMarker(new MarkerOptions().position(latLng).title("updated!")); //marker title never updates
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
}
}
@Override
public void onButtonClicked(int which) {
switch (which) {
case DialogManager.BUTTON_POSITIVE:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
break;
}
}
}
答案 0 :(得分:2)
您必须调用requestLocationUpdates来获取位置更新。在设置地图功能中使用此功能以及其他功能。
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location arg0) {
Toast.makeText(getActivity().getApplicationContext(),"sssCHANGEDsss", Toast.LENGTH_LONG).show(); //this never appear
if(location != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
gMap.animateCamera(CameraUpdateFactory.zoomTo(20));
gMap.addMarker(new MarkerOptions().position(latLng).title("updated!")); //marker title never updates
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
}
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
更新:我刚刚注意到您正在为您的活动实施LocationListerner。如果您计划使用上述解决方案,则无需为Activity实现LocationListener。从更广泛的角度来看,尤其是当您需要通过应用程序进行位置更新时,请创建自定义位置监听器类。联合活动,明智地使用。
不要忘记将ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限添加到AndoidManifest.xml。
答案 1 :(得分:0)
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getActivity().getApplicationContext(),"sssCHANGEDsss", Toast.LENGTH_LONG).show(); //this never appear
if(location != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
gMap.animateCamera(CameraUpdateFactory.zoomTo(20));
gMap.addMarker(new MarkerOptions().position(latLng).title("updated!")); //marker title never updates
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
}
}