2017-06-20 105 views
0

我想比较我当前位置的经度和纬度与另一个纬度和经度。 我的代码有没有错误,但是当我的位置,通过给定的纬度和经度位置管理器获取和比较位置

这里没有任何反应是我的代码:

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

回答

0

你有没有加入你的清单中的谷歌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; 
+0

是我添加的关键,地图是工作,但当时我的位置通过指定纬度和长什么也没有发生。 –

+0

你可以检查我的更新答案。你也不能检查一个位置是否等于另一个位置。你可以检查接近度,并根据它进行计算。 –