0
我对我的项目有一些粗略的代码,我试图在按下按钮时获取用户的GPS位置,并且我希望位置是一个GeoPoint,然后我可以将其传递给方法。如何在单击按钮上获取GPS位置?
该方法适用于绘制叠加点的Overlay类。我正在使用Stack Overflow的一些代码问题How can I obtain the current GPS location?。我设置了程序,以便将GeoPoint添加到数组中,然后从该数组中将其重新添加并添加到方法中。有没有更好的方法来做到这一点,或者有什么方法可以不使用数组来放置点?
这是其中添加所述覆盖按钮和GeoPoint的从阵列得到:
Button startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getLocation();
tempOverlay = (MapOverlay) listOfOverlays.get(arrayNumber);
tempOverlay.addPoint(points.get(pointCount));
mapView.postInvalidate();
}
}
});
这是创建的LocationManager和requestUpdates所述的getLocation()方法。
public void getLocation()
{
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
System.out.println("Get Location Called");
}
这是MyLocationListener
类获取更新里面的onLocationChanged
方法,一旦我有一个位置,我停止更新的要求。
@Override
public void onLocationChanged(Location loc)
{
if(loc != null)
{
Toast.makeText(getBaseContext(),"LocationChanged : Lat: " + loc.getLatitude()+ " Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
}
p = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));
points.add(p);
System.out.println("Point Added to Array");
LBServicesActivity.this.lm.removeUpdates(this);
mc.animateTo(p);
mc.setZoom(18);
}
如何将该GeoPoint传递给buttonListener中的方法而不将其添加到数组中?
嗨对不起,我拿了值这么久才能回复。感谢这个想法,我试了一下,它的工作非常好。再次感谢。 – 2012-04-13 19:12:29