本文介绍了onLocationChanged不起作用,它的接缝没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android非常陌生,我正在开发一个学校项目,我必须使用Google地图。另外,我必须跟踪用户的位置,当位置发生变化时,我必须更新数据库中的用户位置。到目前为止,我设法整合了gmaps,并且我正在努力改变位置。我在我的类上实现了接口LocationListener,我也忽略了onLocationChanged方法,该方法无法工作。我错过了什么?谢谢!

I am pretty new to android, I'm working on an school project where I have to use google maps. Also I have to track the user location and when location is changed, I have to update user location in database. So far I managed to integrate gmaps and I'm working now on location change. I implement the interface LocationListener on my class, also I overrided onLocationChanged method, which seams that is not working. What I'm missing? Thank you!

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以获取位置更新。

You have to call the requestLocationUpdates to get location updates. Use this in your setup map function along with your other functions.

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

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

update: I just noticed that you are implementing the LocationListerner to your activity also. If you are planning to use the above solution, then no need to implement LocationListener to the Activity. On a broader perspective, especially when you need location updates through out the app, create a custom location listener class. un couple from the activities and use wisely.

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

And don't forget to add ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions to your AndoidManifest.xml.

这篇关于onLocationChanged不起作用,它的接缝没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:36
查看更多