**我有一张地图,我添加了一些标记和半径的本地化 - 我想要一些代码让我知道我是否在半径范围内**
这是我的代码:
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);
}
}
答案 0 :(得分:1)
只需使用getActivity()
代替this
答案 1 :(得分: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);
}
答案 2 :(得分:1)
在活动中,您必须使用this
来接受上下文,游标 ..等等
但在片段中,我们使用getActivity()
代替。
答案 3 :(得分: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)