检查我在里面或圈外的半径

问题描述 投票:-7回答:4

**我有一个地图,我添加一些本地化具有带有标记的半径 - 我想一些代码,让我知道,如果我在半径与否**

这是我的代码:

public class FirstFragment extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout1);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    public void buttonClicked(View v) {

    switch (v.getId()) {
     case R.id.gps:
                if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_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.
                    return;
                }
    //Enable the GPS
                mMap.setMyLocationEnabled(true);
        }}
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // first place where i want Marker
        // first place where i want Radius
        LatLng COLLEGE = new LatLng(36.848339, 10.268338);
        Marker college = mMap.addMarker(new MarkerOptions().position(COLLEGE));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(COLLEGE));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(COLLEGE));
    //add the circle
        CircleOptions circleOptions = new CircleOptions();
     circleOptions.center(COLLEGE);
        circleOptions.radius(1000);//add the radius
        mMap.addCircle(circleOptions);

        // first place where i want Marker
        // first place where i want Radius

        LatLng MAR = new LatLng(36.806495, 10.181532);
        Marker mar = mMap.addMarker(new MarkerOptions().position(MAR));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(MAR));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(MAI));
      //add the circle
        CircleOptions circle = new CircleOptions();
        circle.center(MAR);
      //add the radius
        circle.radius(1000);
        mMap.addCircle(circle);
    }}

总是当我搜索一下做一些事情时,我在里面的圆半径,我可以理解这段代码的效果,我不知道在哪里可以在我的FirstFragment插入

 public void onLocationChanged(Location location)
    {
    float [] distance = new float[];
    Location.distanceBetween(location.getLatitude(),
    location.getLongitude(),-6.x,106.xx,distance);

       if (distance[0] < 50)
       {
          Intent i = new Intent(student_activity.this,indoor.class);
          student_activity.this,startActivity(i);
       }
  }
android google-maps google-maps-markers geometry
4个回答
1
投票

在活动中,你必须使用this访问上下文,cursor..etc

但在片段,我们使用getActivity()代替。


1
投票

只需使用getActivity()代替this


1
投票

使用getActivity()

      if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_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. return; } mgoogleMap.setMyLocationEnabled(true);
            }      

1
投票

使用getActivity()和ContextCompact的片段:

替换此:

 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)

附:

if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
© www.soinside.com 2019 - 2024. All rights reserved.