Code Monkey home page Code Monkey logo

Comments (3)

ocram avatar ocram commented on June 14, 2024
  1. Did you follow all steps from the README?
  2. Can you log something in the onPositionChanged callback?
  3. Can you show your call to the SimpleLocation constructor?

from android-simplelocation.

Sidhartha03 avatar Sidhartha03 commented on June 14, 2024
  1. yes I have followed the readme.Please go through the following code.
    my activity code :public class MainActivity extends AppCompatActivity {

    Button btn_location;
    private SimpleLocation location;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     btn_location = (Button) findViewById(R.id.btn_location);
    
     // construct a new instance of SimpleLocation
     location = new SimpleLocation(this);
     // if we can't access the location yet
     if (!location.hasLocationEnabled()) {
         // ask the user to enable location access
         SimpleLocation.openSettings(MainActivity.this);
     }
    
     btn_location.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             if (!location.hasLocationEnabled()) {
                 // ask the user to enable location access
                 SimpleLocation.openSettings(MainActivity.this);
             }
             final double latitude = location.getLatitude();
             final double longitude = location.getLongitude();
    
             if (latitude != 0.0 && longitude != 0.0) {
    
                 Log.d("LOCATION", "LATITUDE :" + latitude);
                 Log.d("LOCATION", "LONGITUDE :" + longitude);
    
                 Toast.makeText(MainActivity.this, "Latitude is :" + latitude + " longitude is :" + longitude, Toast.LENGTH_SHORT).show();
    
    
             }else{
                 Log.d("LOCATION", "LATITUDE :" + latitude);
                 Log.d("LOCATION", "LONGITUDE :" + longitude);
                 location.beginUpdates();
             }
         }
     });
    

    }
    @OverRide
    protected void onResume() {
    super.onResume();
    if (!location.hasLocationEnabled()) {
    // ask the user to enable location access
    SimpleLocation.openSettings(this);
    }

     // make the device update its location
     location.beginUpdates();
    

    }
    @OverRide
    protected void onPause() {
    // stop location updates (saves battery)
    location.endUpdates();
    super.onPause();
    }
    }

from android-simplelocation.

ocram avatar ocram commented on June 14, 2024

First, you should be able to safely remove the if (!location.hasLocationEnabled()) branch and its contents from onResume because you have it in onCreate already.

Second, the additional call to location.beginUpdates() in the OnClickListener in onCreate is probably superfluous.

Otherwise, this looks good. But why don’t you implement the onPositionChanged callback as shown in the README and try to log something there. That way, you would see if and when location updates are available.

Last but not least, you should try the extended arguments of the constructor, again, as shown in the README. For example, try replacing

location = new SimpleLocation(this);

with

location = new SimpleLocation(this, true, false, 5 * 1000, true);

and see if that helps. It should attempt to update the location every 5 seconds.

from android-simplelocation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.