如果目的地是固定的,当用户走路时,我实时计算地图中2点之间的距离

时间:2014-11-09 15:07:16

标签: android google-maps geolocation

我试图在用户走路时在两个坐标之间实时计算距离。我正在使用一个挂起我的应用程序的while循环。我不知道如何纠正它。任何人都可以建议替代它。

package com.ankur.mapdemo;
import android.app.Activity;import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;


public class AlertReminder extends Activity implements OnCheckedChangeListener,LocationListener {

CheckBox cb1,cb2,cb3,cb4,cb5,cb6,cb7;
TextView tv1,tv2;

double currentlatitude=0,currentlongitude=0;
float dis = 0;
Location mostrecent = new Location("");


LocationManager locationManager;
LocationListener locationListener;
Context context;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alertreminder);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  /*  
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };


    */
    // Register the listener with the Location Manager to receive location updates
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);

    tv2= (TextView)findViewById(R.id.location1);

    tv1 = (TextView)findViewById(R.id.currentlocation);

    cb1 = (CheckBox)findViewById(R.id.sjt);
    cb1.setOnCheckedChangeListener(this);

    cb2 = (CheckBox)findViewById(R.id.tt);
    cb2.setOnCheckedChangeListener(this);

    cb3 = (CheckBox)findViewById(R.id.smv);
    cb3.setOnCheckedChangeListener(this);

    cb4 = (CheckBox)findViewById(R.id.mb);
    cb4.setOnCheckedChangeListener(this);

    cb5 = (CheckBox)findViewById(R.id.gdn);
    cb5.setOnCheckedChangeListener(this);

    cb6 = (CheckBox)findViewById(R.id.cdmm);
    cb6.setOnCheckedChangeListener(this);

    cb7 = (CheckBox)findViewById(R.id.allmart);
    cb7.setOnCheckedChangeListener(this);


}



@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    currentlatitude = location.getLatitude();
    currentlongitude = location.getLongitude();

    makeuseofnewlocation(location);
    //newlocation(location);

    tv1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude() + ", Distance:" + dis);

}

private void makeuseofnewlocation(Location location1) {
    // TODO Auto-generated method stub

    mostrecent = location1;
    //currentlatitude = location.getLatitude();
    //currentlongitude = location.getLongitude();
}



@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    Log.d("Latitude","status");
}



@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub

    int i = arg0.getId();

    if(i == R.id.allmart)
    {
        if(arg1)
        {
            double destLat = 12.972887;
            double destLong = 79.159715;

            Location me   = new Location("");
            Location dest = new Location("");


            me.setLatitude(currentlatitude);
            me.setLongitude(currentlongitude);

            dest.setLatitude(destLat);
            dest.setLongitude(destLong);

            float dist = me.distanceTo(dest);

            while(dist > 50)
            {

                makeuseofnewlocation(mostrecent);
                //cbskjcbkcb
                //cbshbcbcbhchj

                //currentlatitude = l1.getLatitude();
                //currentlongitude = l1.getLongitude();

                me.setLatitude(mostrecent.getLatitude());
                me.setLongitude(mostrecent.getLongitude());

                dest.setLatitude(destLat);
                dest.setLongitude(destLong);

                dist = me.distanceTo(dest);
                dis = dist;

            }



            if(dist <= 50)
            {
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


                Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.notify)
                .setContentTitle("ALERT")
                .setContentText("this is a notification")
                 .setSound(soundUri); 

                notificationManager.notify(0, mBuilder.build());
            }

        }
    }




}

} `

1 个答案:

答案 0 :(得分:0)

只要距离大于50,它就会保留在循环中,但不会更新布局中的任何内容。我会为这个计算创建一个新线程(谷歌多线程),让你的主线程只获取这些值并在你的布局中更新它们。