0
アンドロイドモバイルで地図を表示できるようになりました。そして、場所を検索するためのボタンを1つ作成してください。アンドロイドで地図エラーの領域を検索中にアプリケーションが突然停止しました
enter code here
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);
canvas.drawBitmap(bitmap, screenPts.x, screenPts.y, null);
return true;
}
}
私のログcatはcontroller.animateTo(p)にエラーを表示します。
上記の行のエラーを解決するにはどうすればよいですか。 地図に検索オプションを指定する方法を教えてください。 誰かが上記のコードのどこに間違いがあるのか教えてもらえますか?
いずれか1つは私のために助けてください – Vinoth