Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid google maps v2 get last know location and always start with network provider
    primarykey
    data
    text
    <p>This Google Maps v2 api confuses me quite a bit. I have implemented it now in some way i would like to use it. I just have two small problems that i don't know how to go about.</p> <p>Sometimes I don't get a location when i start the app with gps enabled or it takes way too long to get one.</p> <p>So I would like it to always load with the internet location provider first to have a very fast location fix as well as always using the getlastknownlocation() first. Cant figure out how to go about it. </p> <p>Here my code:</p> <pre><code>public class MapViewMain extends FragmentActivity implements LocationListener, LocationSource { private GoogleMap mMap; private OnLocationChangedListener mListener; private LocationManager locationManager; final int RQS_GooglePlayServices = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapviewmain); //start power button service Intent intent=new Intent("com.epicelements.spotnsave.START_SERVICE"); this.startService(intent); int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available Toast.makeText(this, "Google Service not found", Toast.LENGTH_LONG).show(); int requestCode = 10; Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); dialog.show(); }else { // Google Play Services are available locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if(locationManager != null) { boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!gpsIsEnabled) { // Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show(); PopIt("No GPS found", "Would you like to go to the settings to activate it?"); } if(gpsIsEnabled) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this); } else if(networkIsEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this); } else { //Show an error dialog that GPS is disabled... } } else { //Show some generic error dialog because something must have gone wrong with location manager. } setUpMapIfNeeded(); } } @Override public void onPause() { if(locationManager != null) { locationManager.removeUpdates(this); } super.onPause(); } @Override public void onResume() { super.onResume(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); locationManager.getBestProvider(criteria, true); if(mListener != null){ } setUpMapIfNeeded(); mMap.setMyLocationEnabled(true); // Enable the my-location layer if(locationManager != null) { mMap.setMyLocationEnabled(true); // Enable the my-location layer mMap.getUiSettings().setMyLocationButtonEnabled(false); } } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } //This is how you register the LocationSource mMap.setLocationSource(this); } } /** * This is where we can add markers or lines, add listeners or move the camera. * &lt;p&gt; * This should only be called once and when we are sure that {@link #mMap} is not null. */ private void setUpMap() { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mMap.setMyLocationEnabled(true); mMap.getUiSettings().setMyLocationButtonEnabled(false); mMap.setPadding(0,60,0,150); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); } @Override public void activate(OnLocationChangedListener listener) { mListener = listener; } @Override public void deactivate() { mListener = null; } @Override public void onLocationChanged(Location location) { if( mListener != null ) { mListener.onLocationChanged( location ); double lat = location.getLatitude(); double lng = location.getLongitude(); LatLng coordinate = new LatLng(lat, lng); //save coordinate SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode Editor editor = pref.edit(); editor.putFloat("lat", (float) location.getLatitude()); editor.putFloat("lng", (float) location.getLongitude()); editor.putFloat("accuracy", (float) location.getAccuracy()); editor.commit(); //marker stuff mMap.clear(); mMap.addMarker(new MarkerOptions() .position(coordinate) .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 16)); } } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub // Toast.makeText(this, "provider disabled", Toast.LENGTH_SHORT).show(); } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub // Toast.makeText(this, "provider enabled", Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub // Toast.makeText(this, "status changed", Toast.LENGTH_SHORT).show(); } // Alarm Dialog if GPS not enabled public void PopIt( String title, String message ){ new AlertDialog.Builder(this) .setTitle( title ) .setMessage( message ) .setCancelable(false) .setPositiveButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { //do stuff onclick of YES //AlertDialog.dismiss(); } }) .setNegativeButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { //do stuff onclick of CANCEL Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } }).show(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload