我正在制作Google地图应用。当我点击我的屏幕时,我想得到一个点的地址,但是当我点击屏幕时,它没有运行,没有任何反应。我该如何解决这个问题?
这是我的代码:
package app.googlemap;
public class GoogleMapActivity extends MapActivity {
MapView mapView;
View zoomView;
MapController mc;
GeoPoint p;
private List<Overlay> mapOverlays;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(MapView)findViewById(R.id.simple_map);
LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mapView.setTraffic(true);
String coordinates[]={"21.036074","105.833636"};
double latiTude=Double.parseDouble(coordinates[0]);
double longiTude=Double.parseDouble(coordinates[1]);
p=new GeoPoint((int)(latiTude*1E6), (int)(longiTude*1e6));
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mc=mapView.getController();
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
public void draw(GeoPoint g,int a ){
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(a);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
public class MapOverlay extends com.google.android.maps.Overlay{
private Bitmap bmp;
public boolean draw(Canvas canvas, MapView mapView, boolean shadow ,long when){
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.t5);
canvas.drawBitmap(bmp, screenPts.x,screenPts.y-50, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
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);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:1)
在Map ofTap上调用此方法。
/ ** *从特定纬度和经度获取地址的方法。 * * / public static String getAddressFromLocation(double latitude, 双重经度,上下文上下文){
String locationAddress = "";
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses != null && !addresses.isEmpty()) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder(
"Address:\n");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
locationAddress = strReturnedAddress.toString();
locationAddress = locationAddress.replace("Address:", "");
}
} catch (IOException e) {
e.printStackTrace();
}
return locationAddress;
}