android 检测地图上的各种圆圈 v2
android detect various circles on a map v2
在我的小应用程序中,我有一张地图,我正在从网络上解析 XML,如下所示:
<place>
<lat>42.827602</lat>
<lon>-1.663957</lon>
<place_name>Place one</place_name>
<snippet>Snippet de place Facebook</snippet>
<thumb>http://www.animage.com/myicon1.png</thumb>
</place>
<place>
<lat>42.830750</lat>
<lon>-1.669064</lon>
<place_name>Place two</place_name>
<snippet>Snippet de place Twitter</snippet>
<thumb>http://www.animage.com/myicon2.png</thumb>
</place>
<place>
<lat>42.825333</lat>
<lon>-1.668232</lon>
<place_name>Place Three</place_name>
<snippet>Snippet de place Skype</snippet>
<thumb>http://www.animage.com/myicon3.png</thumb>
</place>
</response>
每次我从 xml 读取 "place" 时,我的 AsyncTask 的 onPostExecute 都会调用在我的应用程序地图上创建新标记和圆圈的方法
例如,如果xml有七个"places",则创建新标记和圆的方法将被调用七次。
Marker aMarker;
Circle aCircle;
//...
public void createNewMarkerAndCircle() {
//...
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
//...
public void onPostExecute(String xml) {
xml = stringBuffer.toString();
try {
Document doc = parser.getDomElement(xml);
NodeList nl0 = doc.getElementsByTagName(KEY_PLACE);
Element e = (Element) nl0.item(lap);
theLat = parser.getValue(e, KEY_LATITUDE);
theLon = parser.getValue(e, KEY_LONGITUDE);
//...
createNewMarkerAndCircle();
//...
到目前为止,一切正常,并且在地图上创建了标记和圆圈。
我的目的是当地图上的用户进入其中一个圆圈的半径内时,获取里面的标记的数据。
我告诉你我在做什么:
我有一个位置监听器,每次更新时,都会检查与当前位置的距离和圆的半径。
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
float[] distance = new float[2];
try {
Location.distanceBetween(
location.getLatitude(),
location.getLongitude(),
aCircle.getCenter().latitude,
aCircle.getCenter().longitude, distance);
if (distance[0] > aCircle.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = aMarker.getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
}
//....
这很好用,但有一个大问题。
当我开始走路线时,仅在我进入从 xml 解析的最后一个 "place" 时检测到,因此,创建了最后一个标记和圆圈。
我明白问题出在哪里,最后创建的标记 (Marker aMarker;) 和圆圈 (Circle aCircle;) 是那些分配了值的......但我不知道如何解决它。
我将不胜感激,几天前我正在寻找解决方案,但没有成功。
感谢和问候。
更多信息:
我找到了其他方法,但问题仍然完全相同:
GeofencingApi 正是为此目的创建的:https://developer.android.com/reference/com/google/android/gms/location/GeofencingApi.html
我认为你弄错了createNewMarkerAndCircle(),请在更新地图时将MarkerOptions 和Circle 设为local else 只显示最后一个实例,这就是只显示最后一个圆圈的原因。
public void createNewMarkerAndCircle() {
//...
MarkerOptions aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Circle aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
the last marker (Marker aMarker;) and circle (Circle aCircle;)
created are those with the values assigned ... But i do not know how
to fix it.
为了检查您创建的所有兴趣点的距离,您需要将它们保存在某种结构中,最简单的是列表,稍后在侦听器中引用它们:
Marker aMarker;
Circle aCircle;
// this will hold the circles
List<Circle> allCircles = new ArrayList<Circle>();
// this will hold the markers
List<Marker> allMarkers = new ArrayList<Marker>();
public void createNewMarkerAndCircle() {
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE )));
// store the new marker in the above list
allMarkers.add(aMarker);
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
// store the created circles
allCircles.add(aCircle);
}
// in the LocationListener iterate over all stored circles and check the distances against each one of them
// as you add the circles and markers in the same method you'll have a correspondence between them in the two lists
// another sensible approach would be to create a custom Area class
// to hold both the Marker and the circle in one single place
// so when you find yourself inside a circle the marker will be at the same position in allMarkers
for (int i = 0; i < allCircles.size(); i++) {
Circle c = allCircles.get(i);
Location.distanceBetween(location.getLatitude(), location.getLongitude(), c.getCenter().latitude, c.getCenter().longitude, distance);
if (distance[0] > c.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = allMarkers.get(i).getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
// you found a circles so you may want to break out of the for loop, break;
}
在我的小应用程序中,我有一张地图,我正在从网络上解析 XML,如下所示:
<place>
<lat>42.827602</lat>
<lon>-1.663957</lon>
<place_name>Place one</place_name>
<snippet>Snippet de place Facebook</snippet>
<thumb>http://www.animage.com/myicon1.png</thumb>
</place>
<place>
<lat>42.830750</lat>
<lon>-1.669064</lon>
<place_name>Place two</place_name>
<snippet>Snippet de place Twitter</snippet>
<thumb>http://www.animage.com/myicon2.png</thumb>
</place>
<place>
<lat>42.825333</lat>
<lon>-1.668232</lon>
<place_name>Place Three</place_name>
<snippet>Snippet de place Skype</snippet>
<thumb>http://www.animage.com/myicon3.png</thumb>
</place>
</response>
每次我从 xml 读取 "place" 时,我的 AsyncTask 的 onPostExecute 都会调用在我的应用程序地图上创建新标记和圆圈的方法
例如,如果xml有七个"places",则创建新标记和圆的方法将被调用七次。
Marker aMarker;
Circle aCircle;
//...
public void createNewMarkerAndCircle() {
//...
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
//...
public void onPostExecute(String xml) {
xml = stringBuffer.toString();
try {
Document doc = parser.getDomElement(xml);
NodeList nl0 = doc.getElementsByTagName(KEY_PLACE);
Element e = (Element) nl0.item(lap);
theLat = parser.getValue(e, KEY_LATITUDE);
theLon = parser.getValue(e, KEY_LONGITUDE);
//...
createNewMarkerAndCircle();
//...
到目前为止,一切正常,并且在地图上创建了标记和圆圈。
我的目的是当地图上的用户进入其中一个圆圈的半径内时,获取里面的标记的数据。 我告诉你我在做什么:
我有一个位置监听器,每次更新时,都会检查与当前位置的距离和圆的半径。
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
float[] distance = new float[2];
try {
Location.distanceBetween(
location.getLatitude(),
location.getLongitude(),
aCircle.getCenter().latitude,
aCircle.getCenter().longitude, distance);
if (distance[0] > aCircle.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = aMarker.getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
}
//....
这很好用,但有一个大问题。
当我开始走路线时,仅在我进入从 xml 解析的最后一个 "place" 时检测到,因此,创建了最后一个标记和圆圈。
我明白问题出在哪里,最后创建的标记 (Marker aMarker;) 和圆圈 (Circle aCircle;) 是那些分配了值的......但我不知道如何解决它。
我将不胜感激,几天前我正在寻找解决方案,但没有成功。
感谢和问候。
更多信息:
我找到了其他方法,但问题仍然完全相同:
GeofencingApi 正是为此目的创建的:https://developer.android.com/reference/com/google/android/gms/location/GeofencingApi.html
我认为你弄错了createNewMarkerAndCircle(),请在更新地图时将MarkerOptions 和Circle 设为local else 只显示最后一个实例,这就是只显示最后一个圆圈的原因。
public void createNewMarkerAndCircle() {
//...
MarkerOptions aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Circle aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
the last marker (Marker aMarker;) and circle (Circle aCircle;) created are those with the values assigned ... But i do not know how to fix it.
为了检查您创建的所有兴趣点的距离,您需要将它们保存在某种结构中,最简单的是列表,稍后在侦听器中引用它们:
Marker aMarker;
Circle aCircle;
// this will hold the circles
List<Circle> allCircles = new ArrayList<Circle>();
// this will hold the markers
List<Marker> allMarkers = new ArrayList<Marker>();
public void createNewMarkerAndCircle() {
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE )));
// store the new marker in the above list
allMarkers.add(aMarker);
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
// store the created circles
allCircles.add(aCircle);
}
// in the LocationListener iterate over all stored circles and check the distances against each one of them
// as you add the circles and markers in the same method you'll have a correspondence between them in the two lists
// another sensible approach would be to create a custom Area class
// to hold both the Marker and the circle in one single place
// so when you find yourself inside a circle the marker will be at the same position in allMarkers
for (int i = 0; i < allCircles.size(); i++) {
Circle c = allCircles.get(i);
Location.distanceBetween(location.getLatitude(), location.getLongitude(), c.getCenter().latitude, c.getCenter().longitude, distance);
if (distance[0] > c.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = allMarkers.get(i).getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
// you found a circles so you may want to break out of the for loop, break;
}