Android 谷歌地图,显示当前用户位置附近的位置
我正在谷歌地图上工作,我已经设法在地图上放置了大约73个来自sqllite数据库的精确定位点,它们显示了当前用户城市中商店的位置 现在我只向用户展示壁橱中的五个位置,而不是全部73个位置。有人对此有想法或解决方案吗。Android 谷歌地图,显示当前用户位置附近的位置,android,google-maps,maps,Android,Google Maps,Maps,我正在谷歌地图上工作,我已经设法在地图上放置了大约73个来自sqllite数据库的精确定位点,它们显示了当前用户城市中商店的位置 现在我只向用户展示壁橱中的五个位置,而不是全部73个位置。有人对此有想法或解决方案吗。public Location geoLat(GeoPoint userGeopoint) public Location geoLat(GeoPoint userGeopoint) { Location location = new Location(""); lo
public Location geoLat(GeoPoint userGeopoint)
public Location geoLat(GeoPoint userGeopoint)
{
Location location = new Location("");
location.setLatitude(userGeopoint.getLatitudeE6()/1E6);
return location;
}
public Location geoLng(GeoPoint userGeopoint)
{
Location location = new Location("");
location.setLatitude(userGeopoint.getLongitudeE6()/1E6);
return location;
}
public GeoPoint userGeopoint(Location location)
{
Double userLat = location.getLatitude()*1E6;
Double userLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(userLat.intValue(), userLng.intValue());
return point;
}
private void nearRSU(Location location)
{
Location userlocation = new Location("point A");
Location objectlocation = new Location("point B");
userlocation.setLatitude(geoLat(userGeopoint(location())).getLatitude());
userlocation.setLongitude(geoLng(userGeopoint(location())).getLongitude());
double rangeconv = 0, ctrange = 0, sumrange = 0, alltimes = 0;
String rgtype = "", tmtype = "";
int a = 0;
String objectaddress = null;
GeoPoint pointB = null;
for (int i = 0 ; i < listLocRSU.size(); i++)
{
GeoPoint pointA = new GeoPoint((int) (listLocRSU.get(i).lat * 1E6),
(int) (listLocRSU.get(i).lng * 1E6));
objectlocation.setLatitude(pointA.getLatitudeE6()/1E6);
objectlocation.setLongitude(pointA.getLongitudeE6()/1E6);
double range = userlocation.distanceTo(objectlocation);
if (range >= 1000)
{
rangeconv = range / 1000;
rgtype = " km";
}
else
{
rangeconv = range;
rgtype = " m";
}
double times = rangeconv / 40;
if (times >= 1)
{
alltimes = times;
tmtype = " h";
}
else
{
alltimes = times * 60;
tmtype = " min";
}
if (ctrange > rangeconv)
{
ctrange = rangeconv;
sumrange = ctrange;
a = i;
pointB = new GeoPoint ((int) (listLocRSU.get(a).lat * 1E6),
(int) (listLocRSU.get(a).lng * 1E6));
}
else if (ctrange < rangeconv)
{
sumrange = ctrange;
a = a;
pointB = new GeoPoint ((int) (listLocRSU.get(a).lat * 1E6),
(int) (listLocRSU.get(a).lng * 1E6));
}
double objLat = listLocRSU.get(a).lat;
double objLng = listLocRSU.get(a).lng;
Geocoder objectgc = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = objectgc.getFromLocation(objLat, objLng, 1);
StringBuilder objaddress = new StringBuilder();
if (addresses.size() > 0)
{
Address address = addresses.get(0);
address.getMaxAddressLineIndex();
objaddress.append(address.getAddressLine(0)).append(", ");
objaddress.append(address.getLocality()).append(", ");
objaddress.append(address.getCountryName()).append(", ");
objaddress.append(address.getPostalCode());
}
objectaddress = objaddress.toString();
}
catch (IOException e){}
List<Overlay> overlays = map.getOverlays();
Drawable marker = this.getResources().getDrawable(R.drawable.marker);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(marker, this);
OverlayItem overlayitem = new OverlayItem(pointB, listLocRSU.get(a).locname,
"Address:\n" + objectaddress +
"\n\nLongitude:\n" + listLocRSU.get(a).lng +
"\n\nLatitude:\n" + listLocRSU.get(a).lat +
"\n\nDistance:\n" + sumrange + rgtype +
"\n\nTime Calculation (40 km/h):\n" + alltimes + tmtype);
itemizedOverlay.addItem(overlayitem);
overlays.add(itemizedOverlay);
}
}
{
位置=新位置(“”);
location.setLatitude(userGeopoint.getLatitudeE6()/1E6);
返回位置;
}
公共位置geoLng(GeoPoint用户GeoPoint)
{
位置=新位置(“”);
location.setLatitude(userGeopoint.getLongitudeE6()/1E6);
返回位置;
}
公共地质点用户地质点(位置)
{
Double userLat=location.getLatitude()*1E6;
Double userLng=location.getLongitude()*1E6;
地质点=新的地质点(userLat.intValue(),userLng.intValue());
返回点;
}
靠近RSU的专用空间(位置)
{
Location userlocation=新位置(“点A”);
位置对象位置=新位置(“B点”);
setLatitude(geoLat(userGeopoint(location()).getLatitude());
setLongitude(geoLng(userGeopoint(location()).getLongitude());
双量程conv=0,ctrange=0,sumrange=0,alltimes=0;
字符串rgtype=“”,tmtype=“”;
int a=0;
字符串objectaddress=null;
地质点B=零;
对于(int i=0;i=1000)
{
rangeconv=范围/1000;
rgtype=“km”;
}
其他的
{
rangeconv=范围;
rgtype=“m”;
}
双倍=rangeconv/40;
如果(次数>=1)
{
alltimes=次;
tmtype=“h”;
}
其他的
{
alltimes=次*60;
tmtype=“min”;
}
如果(ctrange>rangeconv)
{
ctrange=rangeconv;
sumrange=ctrange;
a=i;
点B=新的地质点((int)(listLocRSU.get(a).lat*1E6),
(int)(listLocRSU.get(a.lng*1E6));
}
否则如果(ctrange0)
{
地址=地址。获取(0);
address.getMaxAddressLineIndex();
objaddress.append(address.getAddressLine(0)).append(“,”);
objaddress.append(address.getLocality()).append(“,”);
objaddress.append(address.getCountryName()).append(“,”);
append(address.getPostalCode());
}
objectaddress=objaddress.toString();
}
捕获(IOE){}
List overlays=map.getOverlays();
Drawable marker=this.getResources().getDrawable(R.Drawable.marker);
MyItemizedOverlay itemizedOverlay=新的MyItemizedOverlay(标记,this);
OverlayItem OverlayItem=新的OverlayItem(pointB,listLocRSU.get(a).locname,
“地址:\n”+对象地址+
“\n\n长度:\n”+listLocRSU.get(a).lng+
“\n\n纬度:\n”+listLocRSU.get(a.lat+
“\n\n立场:\n”+sumrange+rgtype+
“\n\n时间计算(40 km/h):\n”+所有时间+tmtype);
项目化覆盖附加项(覆盖项);
叠加。添加(itemizedOverlay);
}
}
首先,计算用户位置和所有位置之间的距离
要获取用户位置,请阅读本指南
使用计算类与用户位置之间距离的方法为您的位置创建类:
public class Place {
public Location location;
private float distanceToUser;
public Place(Location _loc){
location = _loc;
}
public void calculateDistance(Location userLocation){
distanceToUser = location.distanceTo(userLocation);
}
}
这里,调用了distanceTo
方法:read
现在,创建Place[]
数组,并使用数据库中的所有位置对其进行初始化。对于每个位置调用calculateInstance
方法
然后,使用数组对数组进行排序。排序()
在地图上仅显示阵列的前五个位置
完成了 谢谢,我会尝试一下,并给你一个反馈谢谢,它起作用了,不是以同样的方式,但我使用了同样的逻辑,即计算距离并确定我是否查看了精确定位