gpt4 book ai didi

android - onLocationChanged 不工作,接缝未被调用

转载 作者:行者123 更新时间:2023-11-29 20:52:09 25 4
gpt4 key购买 nike

我是 android 的新手,我正在做一个必须使用谷歌地图的学校项目。我还必须跟踪用户位置,当位置发生变化时,我必须更新数据库中的用户位置。到目前为止,我设法集成了 gmaps,现在我正在处理位置更改。我在我的类上实现了 LocationListener 接口(interface),我还覆盖了 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;
}
}
}

最佳答案

您必须调用 requestLocationUpdates 来获取位置更新。在您的设置 map 函数中将其与其他函数一起使用。

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);

更新:我刚刚注意到您也在为您的 Activity 实现 LocationListerner。如果您打算使用上述解决方案,则无需为 Activity 实现 LocationListener。从更广泛的角度来看,尤其是当您需要通过应用程序更新位置时,创建一个自定义位置监听器类。从 Activity 中解脱出来并明智地使用。

并且不要忘记将 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限添加到您的 AndoidManifest.xml。

关于android - onLocationChanged 不工作,接缝未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28854635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com