可拉拔电缆不';t在Android的mapView中,在触摸屏上更改其位置。怎么做?
这是我处理位置的代码。每次我触摸地图上的某个地方时,我都想更改可绘制的内容。虽然,我得到了位置,但可拖动的仍然是固定的。怎么做可拉拔电缆不';t在Android的mapView中,在触摸屏上更改其位置。怎么做?,android,Android,这是我处理位置的代码。每次我触摸地图上的某个地方时,我都想更改可绘制的内容。虽然,我得到了位置,但可拖动的仍然是固定的。怎么做 public class LocationActivity extends MapActivity{ Bundle bundle = new Bundle(); MapView mapView; MapController mc; double lat,lon; GeoPoint p; ArrayList <S
public class LocationActivity extends MapActivity{
Bundle bundle = new Bundle();
MapView mapView;
MapController mc;
double lat,lon;
GeoPoint p;
ArrayList <String> address = new ArrayList<String>();
List<Address> addresses;
Location location;
private LocationManager locationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.mapView1);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
String strLocationProvider = lm.getBestProvider(criteria, true);
mc = mapView.getController();
try {
location = lm.getLastKnownLocation(strLocationProvider);
if (location != null) {
double lon = (double) location.getLongitude();
double lat = (double) location.getLatitude();
//lat = 20.00;
// lon = 52.00;
p = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
mc.animateTo(p);
mapView.displayZoomControls(true);
mc.setZoom(17);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
Geocoder gcd = new Geocoder(this, Locale.getDefault());
try {
addresses = gcd.getFromLocation(lat,lon,1);
if (addresses.size() > 0 && addresses != null) {
address.add(addresses.get(0).getFeatureName());
address.add(addresses.get(0).getLocality());
address.add(addresses.get(0).getAdminArea());
address.add(addresses.get(0).getCountryName());
bundle.putStringArrayList("id1", address);
}
bundle.putDouble("lat", lat);
bundle.putDouble("lon", lon);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.logo);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
Bundle bundle = new Bundle();
ArrayList <String> address = new ArrayList<String>();
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
addOverLay();
String add = "";
if (addresses.size() > 0)
{
address.add(addresses.get(0).getFeatureName());
address.add(addresses.get(0).getLocality());
address.add(addresses.get(0).getAdminArea());
address.add(addresses.get(0).getCountryName());
bundle.putStringArrayList("id1", address);
for(int i = 0; i <= addresses.size();i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
bundle.putDouble("lat", p.getLatitudeE6() / 1E6);
bundle.putDouble("lon", p.getLongitudeE6() / 1E6);
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
public void onClick_mapButton(View v)
{
Intent intent = this.getIntent();
this.setResult(RESULT_OK, intent);
intent.putExtras(bundle);
finish();
}
public void addOverLay()
{
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
公共类LocationActivity扩展了MapActivity{
Bundle=新Bundle();
地图视图;
地图控制器;
双lat,lon;
地质点p;
ArrayList地址=新的ArrayList();
列出地址;
位置;
私人场所经理场所经理;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView=(mapView)findViewById(R.id.mapView1);
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
标准=新标准();
//标准.设定准确度(标准.准确度/精细度);
标准。setAltitudeRequired(false);
标准。需要设置(假);
条件.setCostAllowed(true);
字符串strLocationProvider=lm.getBestProvider(条件为true);
mc=mapView.getController();
试一试{
位置=lm.getLastKnownLocation(strLocationProvider);
如果(位置!=null){
double lon=(double)location.getLongitude();
double lat=(double)location.getLatitude();
//lat=20.00;
//lon=52.00;
p=新的地质点((内部)(纬度*1E6),(内部)(纬度*1E6));
司马迁(p),;
mapView.DisplayZoomControl(真);
mc.setZoom(17);
MapOverlay MapOverlay=新的MapOverlay();
List ListoForLays=mapView.getOverlays();
listOfOverlays.clear();
添加(映射覆盖);
Geocoder gcd=新的Geocoder(这个,Locale.getDefault());
试一试{
地址=gcd.getFromLocation(lat,lon,1);
if(addresses.size()>0&&addresses!=null){
address.add(address.get(0.getFeatureName());
address.add(address.get(0.getLocation());
address.add(address.get(0.getAdminArea());
address.add(address.get(0.getCountryName());
bundle.putStringArrayList(“id1”,地址);
}
捆绑。双套(“lat”,lat);
束,双倍(“lon”,lon);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}
}
类MapOverlay扩展了com.google.android.maps.Overlay
{
@凌驾
公共布尔绘制(画布、地图视图、地图视图、,
布尔阴影(长时)
{
super.draw(画布、地图视图、阴影);
//---将地质点转换为屏幕像素---
点屏幕PTS=新点();
getProjection().toPixels(p,screenPts);
//---添加标记---
位图bmp=BitmapFactory.decodeResource(
getResources(),R.drawable.logo);
canvas.drawBitmap(bmp,screenPts.x,screenPts.y-50,null);
返回true;
}
@凌驾
公共布尔onTouchEvent(MotionEvent事件,MapView MapView)
{
//---当用户抬起手指时---
如果(event.getAction()==1){
Bundle=新Bundle();
ArrayList地址=新的ArrayList();
地理点p=mapView.getProjection().fromPixels(
(int)event.getX(),
(int)event.getY();
地理编码器地理编码器=新地理编码器(
getBaseContext(),Locale.getDefault();
试一试{
列表地址=geoCoder.getFromLocation(
p、 getLatitudeE6()/1E6,
p、 getLongitudeE6()/1E6,1);
addOverLay();
字符串add=“”;
如果(地址.size()>0)
{
address.add(address.get(0.getFeatureName());
address.add(address.get(0.getLocation());
address.add(address.get(0.getAdminArea());
address.add(address.get(0.getCountryName());
bundle.putStringArrayList(“id1”,地址);
对于(int i=0;i