如何从Android中的谷歌地图获取搜索位置

时间:2012-03-31 11:15:45

标签: android google-maps

在我的代码中,我有一个文本框和一个按钮。无论您在文本框中输入什么位置,都必须在单击按钮时从Google地图中搜索该位置。并且必须向搜索到的地方显示图标 在我的代码中我得到了地图,但我无法搜索该位置。请帮忙。

  public class MapActivity extends com.google.android.maps.MapActivity implements
    OnClickListener {
/** Called when the activity is first created. */
MapView view;
Button search;
EditText location;
MapController controller;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    view = (MapView) findViewById(R.id.themap);
    location = (EditText) findViewById(R.id.editText1);
    search = (Button) findViewById(R.id.search);
    search.setOnClickListener(this);
    view.setBuiltInZoomControls(true);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == search) {
        Geocoder geo = new Geocoder(getApplicationContext(),
                Locale.getDefault());
        try {
            List<Address> addresses = geo.getFromLocationName(location
                    .getText().toString(), 5);
            if (addresses.size() > 0) {
                GeoPoint p = new GeoPoint((int) (addresses.get(0)
                        .getLatitude() * 1E6), (int) (addresses.get(0)
                        .getLongitude() * 1E6));

                controller.animateTo(p);
                controller.setZoom(12);
                MapOverlay mapOverlay = new MapOverlay();
                List<Overlay> lisOverlays = view.getOverlays();
                lisOverlays.clear();
                lisOverlays.add(mapOverlay);

            } else {
                AlertDialog.Builder adb = new AlertDialog.Builder(
                        MapActivity.this);
                adb.setTitle("Google Map");
                adb.setMessage("please provide proper place");
                adb.setPositiveButton("Close", null);
                adb.show();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
 }
  MapOverlay.java
   public class MapOverlay extends com.google.android.maps.Overlay
   {
Context context;
public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
{
    super.draw(canvas, mapView, shadow);
    Point screenPts = new Point();
    GeoPoint p = null;
    mapView.getProjection().toPixels(p, screenPts);
    Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),     R.drawable.ic_launcher);
    return true;

}

}

编译时,我收到错误:controller.animateTo(p);

1 个答案:

答案 0 :(得分:0)

In oncreate() instantiate the Mapcontroller as below so that you will not get an error in animateTo method.

controller= view.getController();

also GeoPioint p =null; so replace the MAp overlay class with below


MapOverlay.java

public class MapOverlay extends com.google.android.maps.Overlay
{
GeoPoint p;
Context context;
public MyOverLay(Context context,GeoPoint p) // GeoPoint is a int. (6E) 
{ 
this.p = p; 
this.context = context;
} 
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);
    Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),     R.drawable.ic_launcher);
    return true;

}

然后在您的主代码中传递如下所示的geopoint它将解决您的问题....

MapOverlay mapOverlay = new MapOverlay(getApplicationContext(),p);