Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中支持谷歌地图中的多个设备_Android_Google Maps - Fatal编程技术网

在android中支持谷歌地图中的多个设备

在android中支持谷歌地图中的多个设备,android,google-maps,Android,Google Maps,我有一个安卓类,在那里我可以定位GPS位置。但是我想知道这是否可以用于定位地图上的许多其他设备。如何实现这一点。 这是我的密码 public class MapDemoActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, L

我有一个安卓类,在那里我可以定位GPS位置。但是我想知道这是否可以用于定位地图上的许多其他设备。如何实现这一点。 这是我的密码

  public class MapDemoActivity extends AppCompatActivity implements
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener {

        private SupportMapFragment mapFragment;
        private GoogleMap map;
        private GoogleApiClient mGoogleApiClient;
        private LocationRequest mLocationRequest;
        private long UPDATE_INTERVAL = 60000;  /* 60 secs */
        private long FASTEST_INTERVAL = 5000; /* 5 secs */

        /*
         * Define a request code to send to Google Play services This code is
         * returned in Activity.onActivityResult
         */
        private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

        @Override
        protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_demo_activity);

            mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
            if (mapFragment != null) {
                mapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap map) {
                        loadMap(map);
                    }
                });
            } else {
                Toast.makeText(this, "Error - Map Fragment was null!!", Toast.LENGTH_SHORT).show();
            }

        }

        protected void loadMap(GoogleMap googleMap) {
            map = googleMap;
            if (map != null) {
                // Map is ready
                Toast.makeText(this, "Map Fragment was loaded properly!", Toast.LENGTH_SHORT).show();
                if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)

                    map.setMyLocationEnabled(true);

                // Now that map has loaded, let's get our location!
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this).build();

                connectClient();
            } else {
                Toast.makeText(this, "Error - Map was null!!", Toast.LENGTH_SHORT).show();
            }
        }

        protected void connectClient() {
            // Connect the client.
            if (isGooglePlayServicesAvailable() && mGoogleApiClient != null) {
                mGoogleApiClient.connect();
            }
        }

        /*
         * Called when the Activity becomes visible.
        */
        @Override
        protected void onStart() {
            super.onStart();
            connectClient();
        }

        /*
         * Called when the Activity is no longer visible.
         */
        @Override
        protected void onStop() {
            // Disconnecting the client invalidates it.
            if (mGoogleApiClient != null) {
                mGoogleApiClient.disconnect();
            }
            super.onStop();
        }

        /*
         * Handle results returned to the FragmentActivity by Google Play services
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // Decide what to do based on the original request code
            switch (requestCode) {

            case CONNECTION_FAILURE_RESOLUTION_REQUEST:
                /*
                 * If the result code is Activity.RESULT_OK, try to connect again
                 */
                switch (resultCode) {
                case Activity.RESULT_OK:
                    mGoogleApiClient.connect();
                    break;
                }

            }
        }

        private boolean isGooglePlayServicesAvailable() {
            // Check that Google Play services is available
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            // If Google Play services is available
            if (ConnectionResult.SUCCESS == resultCode) {
                // In debug mode, log the status
                Log.d("Location Updates", "Google Play services is available.");
                return true;
            } else {
                // Get the error dialog from Google Play services
                Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);

                // If Google Play services can provide an error dialog
                if (errorDialog != null) {
                    // Create a new DialogFragment for the error dialog
                    ErrorDialogFragment errorFragment = new ErrorDialogFragment();
                    errorFragment.setDialog(errorDialog);
                    errorFragment.show(getSupportFragmentManager(), "Location Updates");
                }

                return false;
            }
        }

        /*
         * Called by Location Services when the request to connect the client
         * finishes successfully. At this point, you can request the current
         * location or start periodic updates
         */
        @Override
        public void onConnected(Bundle dataBundle) {
            // Display the connection status
            Location location = null;
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)


                location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (location != null) {
                Toast.makeText(this, "GPS location was found!", Toast.LENGTH_SHORT).show();
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
                map.animateCamera(cameraUpdate);
                startLocationUpdates();
            } else {
                Toast.makeText(this, "Current location was null, enable GPS on emulator!", Toast.LENGTH_SHORT).show();
            }
        }

        protected void startLocationUpdates() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            mLocationRequest.setInterval(UPDATE_INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                    mLocationRequest, this);
        }

        public void onLocationChanged(Location location) {
            // Report to the UI that the location was updated
            String msg = "Updated Location: " +
                    Double.toString(location.getLatitude()) + "," +
                    Double.toString(location.getLongitude());
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

        }

        /*
         * Called by Location Services if the connection to the location client
         * drops because of an error.
         */
        @Override
        public void onConnectionSuspended(int i) {
            if (i == CAUSE_SERVICE_DISCONNECTED) {
                Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
            } else if (i == CAUSE_NETWORK_LOST) {
                Toast.makeText(this, "Network lost. Please re-connect.", Toast.LENGTH_SHORT).show();
            }
        }

        /*
         * Called by Location Services if the attempt to Location Services fails.
         */
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            /*
             * Google Play services can resolve some errors it detects. If the error
             * has a resolution, try sending an Intent to start a Google Play
             * services activity that can resolve error.
             */
            if (connectionResult.hasResolution()) {
                try {
                    // Start an Activity that tries to resolve the error
                    connectionResult.startResolutionForResult(this,
                            CONNECTION_FAILURE_RESOLUTION_REQUEST);
                    /*
                     * Thrown if Google Play services canceled the original
                     * PendingIntent
                     */
                } catch (IntentSender.SendIntentException e) {
                    // Log the error
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplicationContext(),
                        "Sorry. Location services not available to you", Toast.LENGTH_LONG).show();
            }
        }

        // Define a DialogFragment that displays the error dialog
        public static class ErrorDialogFragment extends DialogFragment {

            // Global field to contain the error dialog
            private Dialog mDialog;

            // Default constructor. Sets the dialog field to null
            public ErrorDialogFragment() {
                super();
                mDialog = null;
            }

            // Set the dialog to display
            public void setDialog(Dialog dialog) {
                mDialog = dialog;
            }
            // Return a Dialog to the DialogFragment.
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                return mDialog;
            }
        }

    }

我想在地图上标出多个移动设备的位置。这是如何实现的。

如果你想有一个显示多个设备的应用程序,比如飞行雷达之类的东西,你可能需要这些设备定期上传/发布它们的位置到服务器,然后让其他设备定期从该服务器获取该信息,并将其显示在客户机中

我希望这是有道理的