本文介绍了如果目标是固定的,那么当用户走路时,我会实时计算地图中2点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


public class AlertReminder extends Activity实现OnCheckedChangeListener,LocationListener {

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

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


LocationManager locationManager;
LocationListener locationListener;
上下文上下文;

保护无效onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.alertreminder);

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

$ * b $ b LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location){
//当网络发现新位置时调用位置提供商。
makeUseOfNewLocation(location);

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

public void onProviderDisabled(String provider){}
};


$ /
//使用位置管理器注册侦听器以接收位置更新
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);


}



@覆盖
public void onLocationChanged(位置位置){
// TODO自动生成的方法存根

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

使用新位置(位置);
// newlocation(location);

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

}

private void makeuseofnewlocation(Location location1){
// TODO自动生成的方法存根

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



$ b @Override
public void onProviderDisabled(String provider){
// TODO自动生成的方法存根
Log.d(Latitude,disable);
}

@Override
public void onProviderEnabled(String provider){
// TODO自动生成的方法存根
Log.d(Latitude , 使能);

$ b @Override
public void onStatusChanged(String provider,int status,Bundle extras){
// TODO自动生成的方法存根
Log .D( 纬度, 状态);



$ b @Override
public void onCheckedChanged(CompoundButton arg0,boolean arg1){
// TODO自动生成的方法存根

int i = arg0.getId();如果(i == R.id.allmart)
{
if(arg1)
{
double destLat = 12.972887;


double destLong = 79.159715;

Location me = new Location();
位置dest =新位置();


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; (dist {
NotificationManager notificationManager =(NotificationManager)上下文中的




$ b。 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(这是通知)
.setSound(soundUri);

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

}
}




}

}
`

解决方案

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


I m trying to calculate distance in real time between 2 co-ordinates as the user walks. I am using a while loop which hangs my application. I have no idea how to rectify it. Can anyone suggest a alternative to it.

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());
            }

        }
    }




}

}`

解决方案

As long as the distance is greater than 50, it will stay inside the loop, but won't update anything in your layout. I would create a new thread for this calculation (google multithreading), and let your main thread only get those values and update them in your layout.

这篇关于如果目标是固定的,那么当用户走路时,我会实时计算地图中2点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:01