【问题标题】:onLocationChanged not working, it seams that is not calledonLocationChanged 不起作用,它接缝未被调用
【发布时间】:2015-03-04 21:08:00
【问题描述】:

我对 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;
        }
    }
}

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您必须调用 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。

    【讨论】:

    • 我在清单中添加了这些权限。但是我在我的代码中做错了吗?实现LocationListener不行吗?
    • 请检查第一部分。您没有使用 requestLocationUpdates 功能。也可以实现 locationlistener。在这种情况下,在请求更新时使用您的类的实例。在那种情况下,我认为你可以使用这个(虽然没有测试)locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, (android.location.LocationListener) this);
    • 应用程序很糟糕。我收到此错误.... ui.fragments.MapFragment 无法转换为 android.location.LocationListener
    • 对于此错误,请尝试将 Fragment 替换为 FragmentActivity 并可以使用 getSupportFragmentManager() 访问功能和资源。
    【解决方案2】:
    @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));
        }
    }
    

    【讨论】:

    • 如果 toast 永远不会出现...我认为该方法没有被调用
    猜你喜欢
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多