地图片段滞后于滚动

时间:2017-04-26 22:29:16

标签: java android performance google-maps android-fragments

我正在使用带有MapView的Google Maps片段,该片段首先获取用户位置并使用它设置标记。但是我注意到当用户滚动地图时出现延迟,就像是帧丢失一样。

我搜索了一些原因,但我想这取决于我的代码。似乎有些不对劲,但我还不知道。

以下是代码:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setFragment(new MapFragment());
}

protected void setFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, fragment);
    fragmentTransaction.commit();
}

MapFragment.java

GoogleMap mGoogleMap;
MapView mMapView;
View mView;
LocationManager mLocationManager;
Location mUserLocation;

private final String TAG_PARSING_STYLE_FAILURE = "ERROR PARSING JSON";

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

    mLocationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mView =  inflater.inflate(R.layout.fragment_map, container, false);
    return mView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = (MapView) mView.findViewById(R.id.testMapView);

    if( mMapView != null){
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }

}

@Override
public void onMapReady(GoogleMap googleMap) {

    MapsInitializer.initialize(getContext());

    mGoogleMap = googleMap;
    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    mUserLocation = getUserLocation();
    LatLng latLng = new LatLng(mUserLocation.getLatitude(), mUserLocation.getLongitude());

    MarkerOptions marker = new MarkerOptions();
    marker.position(latLng);
    marker.title("My Location");

    mGoogleMap.addMarker(marker);

    CameraPosition cameraPosition = CameraPosition.builder().target(latLng).zoom(17).build();

    mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}



public Location getUserLocation(){
    Location loc = null;
    try {
        loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    } catch (SecurityException ex) {
        Log.e("ERROR - LOCATION", ex.toString());
    }
    return loc;
}

使用MapFragments时有更好的方法吗?

0 个答案:

没有答案