通过以下代码我得到了地图中的搜索位置,我给了一个静态位置的标记它显示正常。当我点击标记我给对话框。通过这个对话框选择我可以将该位置移动到Android中的自定义列表视图。这是我在给定代码中使用btnSearch显示地图的位置
package com.commonsware.android.nooer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
public class NooYawk extends MapActivity {
MapView mapView;
MapController mapController;
GeoPoint mgeoPoint;
Drawable marker;
MyLocationOverlay mLocationOverlay;
MotionEvent e;
public void changeMap(String area)
{
MapController mc=mapView.getController();
GeoPoint myLocation=null;
double lat = 0;
double lng = 0;
try
{
Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result=g.getFromLocationName(area, 1);
if(result.size()>0){
Toast.makeText(NooYawk.this, "country: " + String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
}
else{
Toast.makeText(NooYawk.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(NooYawk.this, "Connection Error", Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
Toast.makeText(NooYawk.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
NooYawk.this.changeMap(area);
}
});
mapView = (MapView) findViewById(R.id.map);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
marker = getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());
mapView.getOverlays().add(new MapOverlay(marker));
mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);
setViewLocation();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case 0:
return new AlertDialog.Builder(this).setTitle("You want Add it to favorites").setIcon(
R.drawable.icon).setPositiveButton("Yes",
new OnClickListener() {
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
}).setCancelable(true).setNegativeButton("Cancel",
new OnClickListener() {
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
})
.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
}).create();
default:
break;
}
return null;
}
private void setViewLocation() {
String[] coordinates = { "22.716221", "75.896816" };
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
mgeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(mgeoPoint);
mapController.setZoom(15);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Point scrPoint;
private GeoPoint getPoint(double lat , double lon) {
return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}
class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {
List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker ) {
super(defaultMarker);
ListofGeopoints.add(new OverlayItem(getPoint(22.716221, 75.896816),
"IN", "India"));
populate();
}
@Override
protected boolean onTap(int index) {
switch (index) {
case 0:
Toast.makeText(getApplicationContext(), "GeoLocation : 0",
Toast.LENGTH_LONG).show();
showDialog(0);
break;
}
return true;
}
String add = "";
List<Address> add_List = new ArrayList<Address>();
private void getAddress() {
/*add_List = ReverseGeocode
.getFromLocation(35.594227, -105.223618, 2);
*/
}
@Override
protected OverlayItem createItem(int i) {
return (ListofGeopoints.get(i));
}
@Override
public int size() {
return ListofGeopoints.size();
}
}
}
请看一次,让我知道我想要更改代码的位置 在此先感谢..