位置经理获取和比较位置

时间:2017-06-20 00:29:23

标签: android google-maps

我想将当前位置的纬度和经度与另一个纬度和经度进行比较。 我的代码没有错误,但当我的位置超过给定的纬度和经度

时没有任何反应

这是我的代码:

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        mMap.setMyLocationEnabled(true);

    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION);
        }

    }

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //Criteria criteria = new Criteria();
    Context context=getApplication();
    String text="WRONG DIRECTION";
    int duration=Toast.LENGTH_SHORT;
    //String provider = lm.getBestProvider(criteria, false);
    Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    Toast toast=Toast.makeText(context, text, duration);
    double c = location.getLatitude();
    double d = location.getLongitude();

    if(c==31.271397&&d==29.999595){

        toast.show();
    }
    else{
        toast.cancel();
    }

1 个答案:

答案 0 :(得分:0)

您是否在清单中添加了Google API密钥?

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIxcahyugsiaVSPmAjyBHldZP-rMdIE3BhgB81fE" />
        //Maps anykey
    </application>

您可以使用distanceto函数比较两个位置

 distanceto = loc1.distanceTo(loc2);

            distanceto = distanceto / 1000;