Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:29:22+00:00 2026-06-13T01:29:22+00:00

Possible Duplicate: Android : Location.distanceTo not working correctly? I am trying to calculate the

  • 0

Possible Duplicate:
Android : Location.distanceTo not working correctly?

I am trying to calculate the distance between the user location(driver or moving location) and a fixed latitude longitude. But however the text does not display the result and just displays TextView.

And whenever the user comes to a certain radius of the fixed latitude longitude the imageview will change to another drawable.I have been trying to do this for over a week now. Any help guys?


Main code:

 public  void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lights);


    //placing location
    LocationManager loc = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = loc.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double clong = location.getLongitude();  
    double clat = location.getLatitude();    //current longitude and latitude

    double latitude=0.305085;
    double longitude=101.70506;     //fixed location

    double dist = Math.sqrt(Math.pow((111.3 * (latitude - clat)),2) + Math.pow((71.5 * (longitude - clong)),2)) * 1000;


if(dist<1000){
    calculate(dist);

    tvDis = (TextView)findViewById(R.id.distance);

    tvDis.setText(dist+"m");

}

 }



 public void calculate(double x){
    ImageView light = (ImageView)findViewById(R.id.imageView1);

    if (x<1000){

        light.setImageResource(R.drawable.icon_yellow);

        if(x<500){
            light.setImageResource(R.drawable.icon_red);
        }
    }
    else{
        light.setImageResource(R.drawable.icon_green);
    }

}

I have tried changing 1000 to a larger number but still there is no luck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:29:23+00:00Added an answer on June 13, 2026 at 1:29 am

    From the developers site

    public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
    

    http://developer.android.com/reference/android/location/Location.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Android Emulator - Trouble creating user accounts I'm trying to register a
Possible Duplicate: Android SMS Receiver not working I am in the beginning stages of
Possible Duplicate: android color between two colors, based on percentage? How to find all
Possible Duplicate: Android: trouble updating to Android SDK Tools, revision 7 I'm trying to
Possible Duplicate: Android HttpClient : NetworkOnMainThreadException I am working on an application that is
Possible Duplicate: How to get the file *.apk location in Android device How should
Possible Duplicate: How do I get the current GPS location programmatically in Android? I
Possible Duplicate: Android Activity Life Cycle - difference between onPause() and OnStop() Can anybody
Possible Duplicate: Android: how to cancel a request of Location update with intent? I
Possible Duplicate: Android NoSuchAlgorithmException: SSLContext SSL implementation not found Has anybody experience with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.