代码之家  ›  专栏  ›  技术社区  ›  Sindhusha Yadavalli

谷歌地图(播放服务:9.0.2)+Firebase(9.0.0)悬挂

  •  0
  • Sindhusha Yadavalli  · 技术社区  · 7 年前

    我正在尝试使用以下方法实现跟踪朋友的代码:

    1. 位置被拉入我的设备,并使用谷歌地图API显示

    上面的第1项在一个月前运行良好,但突然应用程序开始挂起。

    请提出一些想法。提前非常感谢。

    build.gradle

    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
        defaultConfig {
            applicationId "com.tracker"
            minSdkVersion 14
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile 'com.android.support:design:23.4.0'
        compile 'com.android.support:recyclerview-v7:23.2.1'
        compile 'com.google.firebase:firebase-database:9.0.0'
        compile 'com.firebase:firebase-client-android:2.4.0'
        compile 'com.google.android.gms:play-services-maps:9.0.2'
        compile 'com.google.android.gms:play-services-location:9.0.2'
    }
    

    活动文件

    public class ReceiverMapsActivity extends Activity implements
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener {
        private GoogleMap googleMap;
        String trackID = "";
        private Firebase curTrackID_FR;
        private GoogleApiClient mGoogleApiClient;
        private LocationRequest mLocationRequest;
        Location firebaseSenderLocation=null,curReceiverLocation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            //Step-1: Basic Initialization
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_receiver_maps);
            Firebase.setAndroidContext(this);
    
            //Step-2: Map Initialization
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            initilizeMap();
    
            //Step-3: Firebase Registration To get Friend's Info
            curTrackID_FR.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Map<String, String> latLngMap = dataSnapshot.getValue(Map.class);
                    if(latLngMap!=null && latLngMap.get("lat")!=null && latLngMap.get("lng")!=null) {
                        Double lat = Double.parseDouble(latLngMap.get("lat"));
                        Double lng = Double.parseDouble(latLngMap.get("lng"));
                        Location curSenderLocation = new Location("SenderLocation");
                        firebaseSenderLocation=curSenderLocation;
                        curSenderLocation.setLatitude(lat);
                        curSenderLocation.setLongitude(lng);
                        updateMapMarker(curSenderLocation);
                    }
                    else{
                        //NULL value received
                    }
                }
                @Override
                public void onCancelled(FirebaseError firebaseError) {
                }
            });
        }
    
        private boolean initilizeMap() {
            try {
                if (googleMap == null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                        }
                        googleMap.setMyLocationEnabled(true);
                        Location myLocation = googleMap.getMyLocation();
                        if (myLocation != null) {
                            curReceiverLocation = myLocation;
                        }
                        googleMap.setMyLocationEnabled(false);
                    }
                    if (googleMap == null) {
                        Log.e(TAG, "initilizeMap():: GoogleMap NULL ");
                        Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        public void updateDistanceAndAddress(Location senderLocation, Location receiverLocation) {
            // Code to update Distance and Address
        }
    
        public void updateMapMarker(Location location) {
            try {
                googleMap.clear();
                googleMap.animateCamera(
                        CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16.5f)
                );
                googleMap.addMarker(
                        new MarkerOptions()
                                .position(new LatLng(location.getLatitude(), location.getLongitude()))
                                .title("Shuttle-Location")
                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                );
                updateDistanceAndAddress(location, curReceiverLocation);
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    
        @Override
        protected void onResume() {
        }
    
        @Override
        protected void onPause() {
            super.onPause();
        }
    
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                curReceiverLocation=location;
            }
            else{
                Log.i(TAG,"onLocationChanged()::  location NULL");
            }
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(5000); // Update location every second
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
             && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) 
                != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this);
        }
    
        @Override
        public void onConnectionSuspended(int i) {
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
       }
    
       @Override
        protected void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
        }
    
        @Override
        protected void onStop() {
            mGoogleApiClient.disconnect();
            super.onStop();
        }
    }
    
    0 回复  |  直到 7 年前