如何在Google Maps V2上获取Circle Bounds

时间:2013-11-19 02:31:08

标签: android google-maps bounds

我试图找出用户的当前位置是否在圆圈内。我现在有这个代码。

  Circle circle = map.addCircle(new CircleOptions()
     .center(new LatLng(14.635594, 121.032962))
     .radius(80)
     .strokeColor(Color.RED)
     );
 map.setOnMyLocationChangeListener(this);
         Location.distanceBetween( 
                circle.getCenter().latitude, circle.getCenter().longitude,pLat,pLong, distance);

            if( distance[0] > circle.getRadius()  ){
                Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
            }





 public void onMyLocationChange(Location location) {
        // TODO Auto-generated method stub
         pLat = location.getLatitude();

            // Getting longitude of the current location
          pLong = location.getLongitude();

即使我明显在圈内,吐司也在外面显示。我试过这段代码

LatLng latlng = new LatLng (pLat,pLong);
QC.contains(latlng);

其中QC是LatLngBounds变量。我想知道Circle是否还有contains功能来了解用户是否在圈内或是否有可能的解决方法。谢谢!

3 个答案:

答案 0 :(得分:1)

将纬度和经度值乘以1e6,bcoz这些值以微米为单位。

more info about microbyte conversion

答案 1 :(得分:0)

如果您正在使用Criteria请尝试此

map.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager)
        getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
        if (location != null)
        {
// this will animate you to the current location
             map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                     new LatLng(location.getLatitude(),location.getLongitude()), 13));

答案 2 :(得分:0)

    protected double latti;
    protected double longi;
    protected double radius;
   1/ MainActivity extends Activity implements  LocationListener
   2/locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
   3///Add circle
     circle = map.addCircle(new CircleOptions()
            .center(new LatLng(47.974593,0.217905))
            .radius(80)
            .strokeColor(Color.CYAN));
}
    4/radius=circle.getRadius();
    5/onLocationChanged(Location location) {
       latti = location.getLatitude();
      longi = location.getLongitude();

      float[] distance = new float[1];
 Location.distanceBetween(latti,longi,circle.getCenter().latitude,circle.getCenter().longit ude, distance);

      showToast(distance[0]);   

}

private void showToast(float distance) {

if(distance>radius)
{
  remarque="Alarm Out Zone";
}
else{
remarque="Inside Zone";
 }
Toast.makeText(getApplicationContext(),"Latitude:"+latti+"\nLongitude:"+longi+"\n"+remarque,Toast.LENGTH_LONG).show();
     }