地图加载缓慢

时间:2016-10-13 15:36:33

标签: android-fragments android-maps-v2

我已完成在Android应用中启用地图的必要步骤,例如使用SHA-1指纹获取API密钥,但地图加载速度非常慢。在我的应用程序中,我正在使用设备位置标记加载地图。我知道得到这个修复需要一段时间,但在我的应用程序中,它需要大约25-30秒。请参阅以下日志:

10-13 08:16:09.717 16078-16078/? I/art: Late-enabling -Xcheck:jni
10-13 08:16:09.767 16078-16085/? E/art: Failed sending reply to debugger: Broken pipe
10-13 08:16:09.769 16078-16085/? I/art: Debugger is no longer active
10-13 08:16:09.769 16078-16085/? I/art: Starting a blocking GC Instrumentation
10-13 08:16:09.870 16078-16078/? W/System: ClassLoader referenced unknown path: /data/app/com.demo.mapinviewpager-1/lib/arm64
10-13 08:16:09.895 16078-16078/? I/InstantRun: Instant Run Runtime started. Android package is com.demo.mapinviewpager, real application class is null.

然后在大约24秒后,地图加载当前位置:

10-13 08:16:33.034 16078-16078/com.demo.mapinviewpager W/System: ClassLoader referenced unknown path: /data/app/com.demo.mapinviewpager-1/lib/arm64
10-13 08:16:33.083 16078-16078/com.demo.mapinviewpager I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
10-13 08:16:33.382 16078-16078/com.demo.mapinviewpager W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-13 08:16:33.880 16078-16078/com.demo.mapinviewpager D/PagerAdapter: Creating FragmentA
10-13 08:16:33.922 16078-16078/com.demo.mapinviewpager D/PagerAdapter: Creating FragmentB
10-13 08:16:33.933 16078-16078/com.demo.mapinviewpager D/PagerAdapter: Creating FragmentC
10-13 08:16:34.007 16078-16078/com.demo.mapinviewpager I/FragmentA: isGooglePlayServicesAvailable SUCCESS
10-13 08:16:34.007 16078-16078/com.demo.mapinviewpager I/FragmentA: Google API client is null so creating one
10-13 08:16:34.265 16078-16078/com.demo.mapinviewpager I/zzai: Making Creator dynamically
10-13 08:16:34.507 16078-16078/com.demo.mapinviewpager W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000004/n/arm64-v8a
10-13 08:16:34.585 16078-16078/com.demo.mapinviewpager I/Google Maps Android API: Google Play services client version: 9452000
10-13 08:16:34.599 16078-16078/com.demo.mapinviewpager I/Google Maps Android API: Google Play services package version: 9683440
10-13 08:16:34.743 16078-16078/com.demo.mapinviewpager D/FLASH: Couldn't find file:  /data/user/0/com.demo.mapinviewpager/files/DATA_ServerControlledParametersManager.data.com.demo.mapinviewpager (No such file or directory)
10-13 08:16:35.043 16078-16078/com.demo.mapinviewpager I/FragmentA: In onCreateView, calling getMapAsync()
10-13 08:16:35.059 16078-16078/com.demo.mapinviewpager E/MapInPagerDemoActivity: LatLong from FragmentA came up empty
10-13 08:16:35.064 16078-16078/com.demo.mapinviewpager E/MapInPagerDemoActivity: LatLong from FragmentA came up empty
10-13 08:16:35.118 16078-17427/com.demo.mapinviewpager I/Adreno: QUALCOMM build                   : 74df444, I409c65498b
                                                                 Build Date                       : 06/22/16
                                                                 OpenGL ES Shader Compiler Version: XE031.08.00.02
                                                                 Local Branch                     : N16
                                                                 Remote Branch                    : 
                                                                 Remote Branch                    : 
                                                                 Reconstruct Branch               : 
10-13 08:16:35.143 16078-17427/com.demo.mapinviewpager I/OpenGLRenderer: Initialized EGL, version 1.4
10-13 08:16:35.143 16078-17427/com.demo.mapinviewpager D/OpenGLRenderer: Swap behavior 1
10-13 08:16:35.193 16078-16078/com.demo.mapinviewpager I/Choreographer: Skipped 81 frames!  The application may be doing too much work on its main thread.
10-13 08:16:35.460 16078-17507/com.demo.mapinviewpager I/b: Sending API token request.
10-13 08:16:35.617 16078-16078/com.demo.mapinviewpager I/FragmentA: In onMapReady, is map loaded yet?
10-13 08:16:35.708 16078-16078/com.demo.mapinviewpager I/FragmentA: In onConnected(), Google API client is:: true
10-13 08:16:35.724 16078-16078/com.demo.mapinviewpager I/MapInPagerDemoActivity: LatLong from FragmentA is: 37.51554646 and long is: -122.26936605
10-13 08:16:35.745 16078-16078/com.demo.mapinviewpager I/FragmentA: ::Google::My current location set::
10-13 08:16:36.234 16078-17507/com.demo.mapinviewpager I/b: Received API Token: <a token number> / Expires in: 432000000ms
10-13 08:16:36.234 16078-17507/com.demo.mapinviewpager I/c: Scheduling next attempt in 431700 seconds.
10-13 08:16:36.243 16078-17507/com.demo.mapinviewpager I/d: Saved auth token
10-13 08:16:36.271 16078-17461/com.demo.mapinviewpager D/NetworkSecurityConfig: No Network Security Config specified, using platform default
10-13 08:16:37.067 16078-17488/com.demo.mapinviewpager W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
10-13 08:16:37.074 16078-17488/com.demo.mapinviewpager W/System: ClassLoader referenced unknown path: 
10-13 08:16:37.082 16078-17488/com.demo.mapinviewpager I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:1
10-13 08:16:37.082 16078-17488/com.demo.mapinviewpager I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 1
10-13 08:16:37.129 16078-17488/com.demo.mapinviewpager D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded
10-13 08:16:37.207 16078-17488/com.demo.mapinviewpager D/GoogleCertificatesImpl: Fetched 172 Google release certificates
10-13 08:16:37.217 16078-17488/com.demo.mapinviewpager D/GoogleCertificatesImpl: Fetched 336 Google certificates
10-13 08:16:38.582 16078-16089/com.demo.mapinviewpager W/art: Suspending all threads took: 5.827ms
10-13 08:16:40.889 16078-16078/com.demo.mapinviewpager D/FragmentA: In onLocationChanged(), sending new lat: 37.5323156 and long: -122.2899224
10-13 08:17:01.934 16078-16078/com.demo.mapinviewpager I/FragmentA: In onConnected(), Google API client is:: true
10-13 08:17:02.011 16078-16078/com.demo.mapinviewpager I/MapInPagerDemoActivity: LatLong from FragmentA is: 37.5323156 and long is: -122.2899224
10-13 08:17:02.013 16078-16078/com.demo.mapinviewpager I/FragmentA: ::Google::My current location set::
10-13 08:17:02.114 16078-16078/com.demo.mapinviewpager D/Fragmenta: In onLocationChanged(), sending new lat: 37.5323156 and long: -122.2899224
10-13 08:17:04.182 16078-16085/com.demo.mapinviewpager W/art: Suspending all threads took: 5.447ms

我正在使用ActivityViewPager加载三个Fragments

MyActivity.java

public class MyDemoActivity extends AppCompatActivity
                implements FragmentA.OnFragmentInteractionListener,
                    FragmentB.OnFragmentInteractionListener {

    private MyPagerAdapter pagerAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_in_pager_demo);

        ViewPager mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setClipToPadding(false);
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        pagerAdapter.notifyDataSetChanged();
        mPager.setAdapter(pagerAdapter);

        // This is required to avoid a black flash when the map is loaded.  The flash is due
        // to the use of a SurfaceView as the underlying view of the map.
        mPager.requestTransparentRegion(mPager);

        //always start w/ Maps View, FragmentA
        mPager.setCurrentItem(1);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        //do something
    }

    @Override
    public void makeUseOfNewLocation(UserLatLong userLatLong) {
         newUserLatLong = userLatLong;
    }


   /**
    * Used for fetching data from activity
    * @return
    */
    public UserLatLong getLLFromActivity() {
        if(newUserLatLong == null) {
           Log.e(TAG, "LatLong from FragmentA came up empty");
        }
        return newUserLatLong;
    }
}

MyPagerAdapter.java

public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FragmentB();
            case 1:
                //map fragment
                return new FragmentA();
            case 2:
                return new FragmentC();
            default:
                return null;
        }
    }

}

FragmentA.java / **完成所有与地图相关的工作* /

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //checks whether play services are installed
    if(!servicesAvailable()) {
        return;
    }

    //Create the location client to start receiving updates
    buildGoogleApiClient();

    if(mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_map, container, false);

    supportMapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);

    if (supportMapFragment != null) {
        supportMapFragment.getMapAsync(this);
    }

    return v;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    // Display the connection status
    try {
        if (ContextCompat.checkSelfPermission( getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( getActivity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED)
        {
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
        } else {
            ActivityCompat.requestPermissions( getActivity(), new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION },
                    TAG_CODE_PERMISSION_LOCATION);
        }
    } catch (SecurityException se) {
        Log.e(TAG, "SecurityException: " + se.getLocalizedMessage());
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    if (mGoogleApiClient != null) {
        try {
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

            if (mCurrentLocation != null) {
                final LatLng latLng = new LatLng(mCurrentLocation.getLatitude(),
                                                    mCurrentLocation.getLongitude());

                //wrap LatLng into UserLatLong
                userLatLong.setLat(latLng.latitude);
                userLatLong.setLng(latLng.longitude);
                //updating the value in the interface
                mLLCallback.makeUseOfNewLocation(userLatLong);

                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 20);
                mMap.animateCamera(cameraUpdate);
                mMap.addMarker(new MarkerOptions().position(latLng));
                Log.i(TAG, "::Google::My current location set::");

                mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                    @Override
                    public void onMapClick(LatLng latLng1) {
                        mMap.clear(); //removes the previous marker
                        mMap.addMarker(new MarkerOptions().position(latLng1));
                        //TODO: update A/C Fragments with these new coordinates
                        float x = (float) latLng1.latitude;
                        float y = (float) latLng1.longitude;
                        Log.d(TAG, "Map clicked w/ lat: " + x + " and long: " + y);

                        //wrap LatLng into UserLatLong
                        userLatLong.setLat(latLng1.latitude);
                        userLatLong.setLng(latLng1.longitude);

                        //updating the value in the interface
                        mLLCallback.makeUseOfNewLocation(userLatLong);
                    }
                });

                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                //for zooming automatically to the location of the marker
                CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(12).build();
                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                //TODO: Constrain the camera target to bounds defined by API
                mMap.setMinZoomPreference(Constants.DEFAULT_MIN_ZOOM);
                mMap.setMaxZoomPreference(Constants.DEFAULT_MAX_ZOOM);

            } else {
                Toast.makeText(getActivity(), "Current location was null so, calling startLocationUpdates()",
                                            Toast.LENGTH_SHORT).show();
                //Begin polling for new location updates
                startLocationUpdates();
            }
        } catch (SecurityException se1) {
            Log.e(TAG, "SecurityException1: " + se1.getLocalizedMessage());
        }

    } else {
        Toast.makeText(getActivity(), "Google API client is null!", Toast.LENGTH_SHORT).show();
    }

}

protected void startLocationUpdates() {
    mLocationRequest = new LocationRequest();
    //TODO: throttle these based on the needs
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(Constants.UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(Constants.FASTEST_INTERVAL);
    try {
        //used for foreground use cases
        //for background use a PendingIntent
        if (ContextCompat.checkSelfPermission( getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( getActivity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED)
        {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        } else {
            ActivityCompat.requestPermissions( getActivity(), new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION },
                    TAG_CODE_PERMISSION_LOCATION);
        }
    } catch (SecurityException see) {
        Log.e(TAG, "SecurityException2: " + see.getLocalizedMessage());
    }
}

0 个答案:

没有答案