在我的小应用程序,我有地图,我从网页解析XML,像这样:每次我读时间Android的检测各界在地图V2
<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>
“地方“,我的AsyncTask的onPostExecute,调用在我的应用程序的地图上创建一个新的标记和圆的方法
例如,如果xml有七个”位置“,则创建一个新的方法标记和一个圆被称为七次。
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解析的最后一个“地点”时才会检测到,因此创建了最后一个标记和圆。
我明白问题是什么,创建的最后一个标记(Marker aMarker;)和圆(Circle aCircle;)是那些赋值的......但我不知道如何解决它。
我希望任何帮助,前几天我正在寻找解决方案,但没有成功。
感谢和问候。
更多信息:
我发现这个办法了,但问题仍然没有完全一样的:
https://gist.github.com/saxman/5347195
您的问题是您不会保留对您生成的圆圈和标记的引用,并且最终引用最后一个引用(因为您只使用引用)。检查这个代码https://gist.github.com/luksprog/de0fc75ead76f0d8881d你可能会这样做(虽然使用geo fencing api会是一个更好的选择)。 – Luksprog 2015-02-09 21:53:38
@Luksprog你的答案是我一直更有用。我会给你150分,但我只有一条评论。请发表一个答案给你的观点。谢谢! – Sergio76 2015-02-13 13:01:46