3

首先,我整个晚上都在寻找答案。我已经阅读了许多问题 - 可能都是 - 询问为什么 OnloCationChanged 不被调用......没有任何帮助。

我有一个显示地图的片段。我可以向它添加标记,它还可以正确显示用户的当前位置。一切似乎都很好,但是我想放大用户的位置,这就是为什么我需要调用“OnLocationChanged”方法。

此外,locationManager.getLastKnownLocation(provider) 始终返回 null。

我已经在真实设备中对此进行了测试。

这是我的配置,我希望有人能指出我正确的方向。

谢谢

片段类

public class MapMainFragment extends BaseFragment implements LocationListener{

private SupportMapFragment fragment;
private GoogleMap map;
private LocationManager locationManager;
private static final long MIN_TIME = 500;
private static final float MIN_DISTANCE = 0;
String provider;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // LOCATION
    locationManager = (LocationManager) mActivity.getSystemService(Context.LOCATION_SERVICE);
    boolean enabledGPS = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enabledWiFi = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    // Check if enabled and if not send user to the GSP settings
    if (!enabledGPS && !enabledWiFi) {
        Toast.makeText(mActivity, "GPS signal not found", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    // Define the criteria how to select the location provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        Toast.makeText(mActivity, "Selected Provider " + provider,
                Toast.LENGTH_SHORT).show();
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE, this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.app_tab_b_second_screen, container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    FragmentManager fm = getChildFragmentManager();
    fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.map, fragment).commit();
    }
}

@Override
public void onResume() {
    super.onResume();

    map = fragment.getMap();
    // Enabling MyLocation Layer of Google Map
    map.setMyLocationEnabled(true);
    // Add Marker
    final LatLng TEST_MARKER = new LatLng(50.34927, 6.26887);
    map.addMarker(new MarkerOptions()
            .position(TEST_MARKER)
            .title("Title")
            .snippet("Sheeps: 4,137,400")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

}

@Override
public void onLocationChanged(Location location) {
    Log.e("onLocationChanged", "called");
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
    map.animateCamera(cameraUpdate);
    locationManager.removeUpdates(this);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(mActivity, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(mActivity, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

BaseFragment 类

public class BaseFragment extends Fragment {
public AppMainTabActivity mActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mActivity       =   (AppMainTabActivity) this.getActivity();
}

public boolean onBackPressed(){
    return false;
}

public void onActivityResult(int requestCode, int resultCode, Intent data){

}
}

清单.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
4

1 回答 1

1

我认为在您的位置更改中它会运行,但是您删除了更新并且它不再运行

在 onlocationchanged 中注释掉这一行

locationManager.removeUpdates(this);

并且您的 getbestprovider 方法使用 true 而不是 false

于 2013-08-19T02:29:51.007 回答