我是编程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();
}
}
}
答案 0 :(得分:0)
:
google.maps.geometry.poly.containsLocation(pos, selectedPolygon);
此方法可在 google-maps API 3 中找到,(听起来你不能)
但是尝试根据圆形区域编写一些数学函数。
真的很容易。计算2个坐标之间的距离:
public static float distFrom2LocationsInMeter() {
double lat1 = ...;
double lng1 = ...;
double lat2 = ...;
double lng2 =...;
//lat1 /= 1000000; // sometimes Android returns location in 10^6 form
//lng1 /= 1000000;
//lat2 /= 1000000;
// lng2 /= 1000000;
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
int meterConversion = 1609;
return Float.valueOf((float)(dist * meterConversion ));
}
检查半径后:
如果R> distFrom2LocationsInMeter()//你在里面
如果R < distFrom2LocationsInMeter()//你在外面
答案 1 :(得分:0)
The problem is during the onStart check of the circles it only detects inside or outside of the last created circle instead of all available ones. I am not sure what is causing this problem.
问题是,当您创建一个圆圈时,您将覆盖最后创建的圆圈,因此startCircle
始终是最后创建的圆圈。您需要保留您在地图上绘制的所有圆圈的列表;
看看如何检查一个点是否在一个对象中我会查看这个链接,因为它在我需要这个时对我非常有用
答案 2 :(得分:0)
Toast.makeText(MapActivity.this,String.valueOf(isInside(latitude_location_place,longitude_location_place,0.002,map.getMyLocation()。getLatitude(),map.getMyLocation()。getLongitude())),Toast.LENGTH_SHORT。 show();
布尔布尔值isInside(double circle_x,double circle_y, double rad,double x,double y){
map.addCircle(new CircleOptions()
.center(new LatLng(circle_x, circle_y))
.radius(200)
//its about 20 meter
// you most set rad=0.002 for 20 meter
.strokeColor(Color.RED)
.fillColor(R.color.color_circle));
map.addMarker(new MarkerOptions().position(new LatLng(x, y)));
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad*rad)
return true;
else
return false;
}
答案 3 :(得分:0)
示例:
Toast.makeText(MapActivity.this,String.valueOf(isInside(latitude_location_place, longitude_location_place,0.002,map.getMyLocation().getLatitude(),map.getMyLocation().getLongitude())) , Toast.LENGTH_SHORT).show();
public boolean isInside(double circle_x, double circle_y,
double rad, double x, double y) {
map.addCircle(new CircleOptions()
.center(new LatLng(circle_x, circle_y))
.radius(200)
//its about 20 meter
// you most set rad=0.002 for 20 meter
.strokeColor(Color.RED)
.fillColor(R.color.color_circle));
map.addMarker(new MarkerOptions().position(new LatLng(x, y)));
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad*rad)
return true;
else
return false;
}