我是 android 编程新手,我正在开发的地图应用程序有问题。该应用程序允许通过单击将圆圈放置在地图上,如果当前位置在圆圈内,则会显示一条消息,如果在圆圈外,也会显示一条不同的消息。问题是在对圆圈进行 onStart 检查期间,它只检测到最后创建的圆圈的内部或外部,而不是所有可用的圆圈。我不确定是什么导致了这个问题。代码片段如下:
// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
locationCount = sharedPreferences.getInt("locationCount", 0);
// Getting stored zoom level if exists else return 0
//String zoom = sharedPreferences.getString("zoom", "0");
// If locations are already saved
if(locationCount!=0){
String lat = "";
String lng = "";
// Iterating through all the locations stored
for(int i=0;i<locationCount;i++){
// Getting the latitude of the i-th location
lat = sharedPreferences.getString("lat"+i,"0");
// Getting the longitude of the i-th location
lng = sharedPreferences.getString("lng"+i,"0");
double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);
startCircle = googleMap.addCircle(new CircleOptions().center(new LatLng (latitude, longitude)).radius(CIRCLE_RADIUS).fillColor(0x55888888));
}
}
public void onStart(){
super.onStart();
//Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Set accuracy of criteria to address level
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
//Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
double lat = myLocation.getLatitude();
double lon = myLocation.getLongitude();
LatLng latlng = new LatLng(lat,lon);
if(startCircle == null){
return;
}
else{
float[] distance = new float[2];
marker = googleMap.addMarker(new MarkerOptions().position(latlng).visible(false));
myLocation.distanceBetween( marker.getPosition().latitude, marker.getPosition().longitude,
startCircle.getCenter().latitude, startCircle.getCenter().longitude, distance);
if( distance[0] < startCircle.getRadius()){
Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
}
}
}