Example usage for android.location Criteria Criteria

List of usage examples for android.location Criteria Criteria

Introduction

In this page you can find the example usage for android.location Criteria Criteria.

Prototype

public Criteria() 

Source Link

Document

Constructs a new Criteria object.

Usage

From source file:com.example.scandevice.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();/*from   ww w  . j a  va2 s  .  c  o m*/
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    // Getting the name of the provider that meets the criteria
    provider = locationManager.getBestProvider(criteria, true);

    if (provider == null && !locationManager.isProviderEnabled(provider)) {

        // Get the location from the given provider
        List<String> list = locationManager.getAllProviders();

        for (int i = 0; i < list.size(); i++) {
            //Get device name;
            String temp = list.get(i);

            //check usable
            if (locationManager.isProviderEnabled(temp)) {
                provider = temp;
                break;
            }
        }
    }
    //get location where reference last.
    Location location = locationManager.getLastKnownLocation(provider);

    if (location == null)
        Toast.makeText(this, "There are no available position information providers.", Toast.LENGTH_SHORT)
                .show();
    else
        //GPS start from last location.
        onLocationChanged(location);

    // Set by <content src="index.html" /> in config.xml
    loadUrl(launchUrl);
}

From source file:com.dwdesign.tweetings.activity.MapViewerActivity.java

protected void getLocationAndCenterMap() {
    LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    final String provider = mLocationManager.getBestProvider(criteria, true);

    Location mRecentLocation = null;

    if (provider != null) {
        mRecentLocation = mLocationManager.getLastKnownLocation(provider);
    } else {//w w w  .  java 2 s  .c o  m
        Toast.makeText(this, R.string.cannot_get_location, Toast.LENGTH_SHORT).show();
    }

    if (mRecentLocation != null && isNativeMapSupported()) {
        NativeMapFragment aFragment = (NativeMapFragment) mFragment;
        aFragment.setCenter(mRecentLocation);
    }
}

From source file:net.quranquiz.ui.QQMap.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);//from   w w  w .  ja  v a  2  s. co m
    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setOnMarkerClickListener((OnMarkerClickListener) this);

    cairo = new LatLng(30.1, 31.45);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabledGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

    // Check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings
    if (!enabledGPS) {
        Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show();
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    }

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        Toast.makeText(this, "Selected Provider " + provider, Toast.LENGTH_SHORT).show();
        meMarker = map.addMarker(
                new MarkerOptions().position(cairo).title("That's Me").snippet("a normal QuranQuiz Node!")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(cairo, 12.0f));

        onLocationChanged(location);
    } else {

        //do something
    }

}

From source file:com.example.amapapplicationtest.MainActivity.java

/** Called when the activity is first created. */
@Override//from w w w . j  a v  a 2s  . c  o  m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initMap();

    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    String provider = lm.getBestProvider(new Criteria(), true);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    if (location != null) {
        googlemap.animateCamera(CameraUpdateFactory
                .newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
                .zoom(17) // Sets the zoom
                .bearing(90) // Sets the orientation of the camera to east
                .tilt(40) // Sets the tilt of the camera to 30 degrees
                .build(); // Creates a CameraPosition from the builder
        googlemap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }

    if (provider == null) {
        onProviderDisabled(provider);
    }
    data = new MarkerDataSource(context);
    try {
        data.open();

    } catch (Exception e) {
        Log.i("hello", "hello");
    }

    List<MyMarkerObj> m = data.getMyMarkers();
    for (int i = 0; i < m.size(); i++) {
        String[] slatlng = m.get(i).getPosition().split(" ");
        LatLng lat = new LatLng(Double.valueOf(slatlng[0]), Double.valueOf(slatlng[1]));
        googlemap.addMarker(
                new MarkerOptions().title(m.get(i).getTitle()).snippet(m.get(i).getSnippet()).position(lat));

        googlemap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

            public void onInfoWindowClick(Marker marker) {
                Toast.makeText(getBaseContext(), "Info Window clicked@" + marker.getId(), Toast.LENGTH_SHORT)
                        .show();
            }
        });

        //  marker.view();
        //    data.getMarker(new MyMarkerObj(marker.getTitle(), marker.getSnippet(), marker.getPosition().latitude + " " + marker.getPosition().longitude));
    }

    googlemap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        public void onMapLongClick(final LatLng latlng) {
            LayoutInflater li = LayoutInflater.from(context);
            final View v = li.inflate(R.layout.alertlayout, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(v);
            builder.setCancelable(false);

            builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    EditText title = (EditText) v.findViewById(R.id.ettitle);
                    EditText snippet = (EditText) v.findViewById(R.id.etsnippet);
                    googlemap.addMarker(new MarkerOptions().title(title.getText().toString())
                            .snippet(snippet.getText().toString())
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                            .position(latlng));
                    String sll = latlng.latitude + " " + latlng.longitude;
                    data.addMarker(
                            new MyMarkerObj(title.getText().toString(), snippet.getText().toString(), sll));
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }
    });
}

From source file:com.example.demo_dv_fuse.MapScreen.java

/**
 * @see android.app.Activity#onCreate(android.os.Bundle)
 *///from w  ww .ja  v a 2s .c o  m
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_screen);
    this.map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
    this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    if (status == ConnectionResult.SUCCESS) { // Google Play Services is available
        // Enabling MyLocation Layer of Google Map
        this.map.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        final Criteria criteria = new Criteria();

        // Getting the name of the best provider
        final String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        final Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            onLocationChanged(location);
            //            } else {
            //                final String coordinates[] = {"1.352566007", "103.78921587"};
            //                final double lat = Double.parseDouble(coordinates[0]);
            //                final double lng = Double.parseDouble(coordinates[1]);
            //
            //                // Creating a LatLng object for the current location
            //                LatLng latLng = new LatLng(lat, lng);
            //
            //                // Showing the current location in Google Map
            //                this.map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            //                this.map.animateCamera(CameraUpdateFactory.zoomTo(15));
        }
        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
        // Better solution would be to display a dialog and suggesting to 
        // go to the settings
        if (!enabledGPS || !enabledWiFi) {
            Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, this);
    } else { // Google Play Services are not available
        final int requestCode = 10;
        final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();
    }
}

From source file:com.findcab.driver.activity.Signup.java

private void initView() {
    context = this;
    back = (Button) findViewById(R.id.back);
    back.setOnClickListener(this);
    start = (Button) findViewById(R.id.start);
    start.setOnClickListener(this);

    edit_name = (EditText) findViewById(R.id.name);
    edit_mobile = (EditText) findViewById(R.id.mobile);
    edit_password = (EditText) findViewById(R.id.password);

    checkBox = (CheckBox) findViewById(R.id.checkBox);

    item = (TextView) findViewById(R.id.item);
    item.setOnClickListener(this);

    if (initGPS()) {

        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) this.getSystemService(serviceName);
        // ?/*from www  .j  a v a 2  s.  co  m*/
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
        }
    }
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.CurrentPosition.java

public CurrentPosition() {
    Prefs prefs = GBApplication.getPrefs();
    this.latitude = prefs.getFloat("location_latitude", 0);
    this.longitude = prefs.getFloat("location_longitude", 0);

    lastKnownLocation = new Location("preferences");
    lastKnownLocation.setLatitude(this.latitude);
    lastKnownLocation.setLongitude(this.longitude);

    LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);

    this.timestamp = System.currentTimeMillis() - 86400000; //let accessor know this value is really old

    if (ActivityCompat.checkSelfPermission(GBApplication.getContext(),
            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && prefs.getBoolean("use_updated_location_if_available", false)) {
        LocationManager locationManager = (LocationManager) GBApplication.getContext()
                .getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = null;/*from w w w  .j  ava 2  s  .c  o m*/
        if (locationManager != null) {
            provider = locationManager.getBestProvider(criteria, false);
        }
        if (provider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
            if (lastKnownLocation != null) {
                this.lastKnownLocation = lastKnownLocation;
                this.timestamp = lastKnownLocation.getTime();
                this.timestamp = System.currentTimeMillis() - 1000; //TODO: request updating the location and don't fake its age

                this.latitude = (float) lastKnownLocation.getLatitude();
                this.longitude = (float) lastKnownLocation.getLongitude();
                this.accuracy = lastKnownLocation.getAccuracy();
                this.altitude = (float) lastKnownLocation.getAltitude();
                this.speed = lastKnownLocation.getSpeed();
            }
        }
    }
}

From source file:com.example.androidmapsv2.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getVistas();/*from w  ww .ja  va 2  s  .  c om*/
    // Establezco los criterios para la localizacin.
    criteriosLocalizacion = new Criteria();
    criteriosLocalizacion.setAccuracy(Criteria.ACCURACY_FINE);
    // Creo un localizador para que me informe del cambio de posicin.
    localizador = (LocationManager) getSystemService(LOCATION_SERVICE);
}

From source file:com.example.android.expandingcells.ExpandingCells.java

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

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the location provider
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); //default

    criteria.setCostAllowed(false);/*from w  w w.  j  av a2 s .c o m*/
    // get the best provider depending on the criteria
    provider = locationManager.getBestProvider(criteria, false);

    // the last known location of this provider
    Location location = locationManager.getLastKnownLocation(provider);

    mylistener = new MyLocationListener();

    if (location != null) {
        mylistener.onLocationChanged(location);
    } else {
        // leads to the settings because there is no last known location
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }
    // location updates: at least 1 meter and 200millsecs change
    //NETWORK_PROVIDER
    locationManager.requestLocationUpdates(provider, 200, 1, mylistener);

    this.NewsList = new ArrayList<News>(4);
    for (int i = 0; i < 4; i++) {
        NewsList.add(new News());
    }

    //Methods to populate the Object
    getColor();
    getHoroscope();
    getRaghukalam();
    //getWeather();

    //Log.d("DEBUG", Byte.getBody());
    Log.d("DEBUG", "Print");
    // Toast.makeText(this, Byte.size(), Toast.LENGTH_LONG).show();

    ExpandableListItem[] values = new ExpandableListItem[] {
            new ExpandableListItem("Color", R.drawable.mb_color, CELL_DEFAULT_HEIGHT,
                    NewsList.get(0).getBody()),
            new ExpandableListItem("Horoscope", R.drawable.mb_horoscope, CELL_DEFAULT_HEIGHT,
                    NewsList.get(1).getBody()),
            new ExpandableListItem("Raghukalam", R.drawable.mb_raghukalam, CELL_DEFAULT_HEIGHT,
                    NewsList.get(2).getBody()),
            new ExpandableListItem("Weather", R.drawable.mb_weather, CELL_DEFAULT_HEIGHT,
                    NewsList.get(3).getBody()), };

    List<ExpandableListItem> mData = new ArrayList<ExpandableListItem>();

    for (int i = 0; i < NUM_OF_CELLS; i++) {
        ExpandableListItem obj = values[i % values.length];
        mData.add(new ExpandableListItem(obj.getTitle(), obj.getImgResource(), obj.getCollapsedHeight(),
                obj.getText()));
    }

    // CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.list_view_item, mData);
    adapter = new CustomArrayAdapter(this, R.layout.list_view_item, mData);

    mListView = (ExpandingListView) findViewById(R.id.main_list_view);
    mListView.setAdapter(adapter);
    mListView.setDivider(null);
}

From source file:com.lauszus.launchpadflightcontrollerandroid.app.MapFragment.java

@Override
public void onResume() {
    super.onResume();
    LocationManager locationManager = (LocationManager) getActivity()
            .getSystemService(Context.LOCATION_SERVICE);
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        new AlertDialog.Builder(getActivity())
                .setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }/*from  w  w w.j av a 2s .c  o  m*/
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int id) {
                        dialog.cancel();
                        Toast.makeText(getActivity(), "GPS must be on in order to use this application!",
                                Toast.LENGTH_LONG).show();
                        getActivity().finish();
                    }
                }).create().show();
    } else {
        setUpMapIfNeeded();
        if (ActivityCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Location location = locationManager
                    .getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
            if (location != null) {
                LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 19.0f));
            }
        }
    }
}