Android 用户在地图上锁定点位置并获取位置';s地址返回到文本框
我在另一个活动中有这个文本框和一个按钮,它指向这个地图。我想做的是,它允许用户单击地图并锁定他们想要的位置,他们还可以将该位置的地址返回到文本框,而无需键入。可以这样做吗?由于现在有一个警报对话框,在用户pin用两个按钮(是和否)指向它后弹出,因此如果是,则将该位置地址提取回文本框。有什么想法吗Android 用户在地图上锁定点位置并获取位置';s地址返回到文本框,android,Android,我在另一个活动中有这个文本框和一个按钮,它指向这个地图。我想做的是,它允许用户单击地图并锁定他们想要的位置,他们还可以将该位置的地址返回到文本框,而无需键入。可以这样做吗?由于现在有一个警报对话框,在用户pin用两个按钮(是和否)指向它后弹出,因此如果是,则将该位置地址提取回文本框。有什么想法吗 static EditText txtLocation; /** Called when the activity is first created. */ @Override
static EditText txtLocation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
List<Overlay> mapOverlays = mapView.getOverlays();
MapOverlay mapOverlay = new MapOverlay();
mapOverlays.add(mapOverlay);
// obtain gps location
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
// LocationManager.GPS_PROVIDER,
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (loc != null) {
Toast.makeText(
getBaseContext(),
"Location changed: Lat: " + loc.getLatitude()
+ " Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
}
p = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
mc.animateTo(p);
mc.setZoom(18);
// Add a location marker
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listofOverlays = mapView.getOverlays();
listofOverlays.clear();
listofOverlays.add(mapOverlay);
// invalidate() method forces the MapView to be redrawn
mapView.invalidate();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlay extends com.google.android.maps.Overlay {
@Override
public boolean onTap(final GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
k = p;
mc = mapView.getController();
mc.animateTo(p);
mapView.invalidate();
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() /1E6,
p.getLongitudeE6() / 1E6, 1
);
String add = "";
if (addresses.size()>0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
// txtLocation.setText(add);
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e){
e.printStackTrace();
}
new AlertDialog.Builder(MapsActivity.this)
.setTitle("Change location..")
.setMessage("go to the new location?")
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
})
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() /1E6,
p.getLongitudeE6() / 1E6, 1
);
String add = "";
if (addresses.size()>0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
txtLocation.setText(add);
// Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e){
e.printStackTrace();
}
}
}).show();
return true;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
if (k != null) {
// ---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(k, screenPts);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.marker);
canvas.drawBitmap(bmp, screenPts.x - 10, screenPts.y - 34, null);
}
return true;
}
}
}
静态编辑文本txtLocation;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mapView=(mapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mc=mapView.getController();
List mapOverlays=mapView.getOverlays();
MapOverlay MapOverlay=新的MapOverlay();
添加(mapOverlays);
//获取gps位置
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新建MyLocationListener();
lm.RequestLocationUpdate(
//LocationManager.GPS\u提供程序,
LocationManager.NETWORK_提供程序,0,0,locationListener);
}
私有类MyLocationListener实现LocationListener{
位置更改后的公共无效(位置loc){
如果(loc!=null){
Toast.makeText(
getBaseContext(),
“位置已更改:纬度:”+loc.getLatitude()
+“Lng:+loc.getLongitude(),
吐司。长度(短)。show();
}
p=新的地质点((int)(位置getLatitude()*1E6),
(int)(loc.getLongitude()*1E6));
司马迁(p),;
mc.setZoom(18);
//添加位置标记
MapOverlay MapOverlay=新的MapOverlay();
List ListoForLays=mapView.getOverlays();
listofOverlays.clear();
添加(映射覆盖);
//方法强制重新绘制MapView
mapView.invalidate();
}
公共无效onProviderDisabled(字符串提供程序){
}
公共无效onProviderEnabled(字符串提供程序){
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
类MapOverlay扩展了com.google.android.maps.Overlay{
@凌驾
公共布尔onTap(最终地质点p,MapView MapView){
//TODO自动生成的方法存根
k=p;
mc=mapView.getController();
司马迁(p),;
mapView.invalidate();
Geocoder Geocoder=新的地理编码器(getBaseContext(),Locale.getDefault());
试一试{
列表地址=geoCoder.getFromLocation(
p、 getLatitudeE6()/1E6,
p、 getLongitudeE6()/1E6,1
);
字符串add=“”;
如果(地址.size()>0)
{
对于(int i=0;i0)
{
对于(int i=0;i您可以尝试反向地理编码。这需要您提供用户点击点的lat长度,这相当容易。
检查类似的问题。可能会有帮助。我尝试了反向地理编码,我将代码放在按钮“是”后。单击“是”后,它会突出显示这一行:。getLastKnownLocation(LocationManager.NETWORK\u PROVIDER);我真的不明白。是什么突出显示了代码的这一部分,如果你的应用程序force关闭,那么logcat给了你什么错误?如果没有错误,那么应该是获取地址。另外,请尝试本教程,详细解释:我要取回地址的文本框属于活动A,此映射是活动Ben在活动a中创建一个给定字符串参数的方法,将textbox设置为该文本,并从活动B中调用此方法。类似于a:public void setAddressText(字符串地址){MyTextView.setText(地址);}和B:ActivityA.setAddressText(AddressFromMap);请澄清您的问题…您想获取被触摸位置或当前位置的地址???删除onTap中的注释代码,您应该传递这些lat、long值以进行反向地理编码…我想将地址获取到用户触摸位置的文本框中。使用textbox.setText(添加)在onTap函数中,我在“是”按钮下使用了它。当我运行它时,它会进入一个特定的句子“txtLocation.setText(add)”如何?txtLocation读取null。txtLocation在另一个活动中。这可能是问题吗?