如何将android地图V1转换为android地图V2?
我有一个android应用程序,它使用android MapV1。现在我想用一些特殊功能更新应用程序,但当我尝试更新应用程序时,MapV1已被弃用 我想将我的应用程序从MapV1转换为V2,请任何人在这方面帮助我 注意:我在V1中使用覆盖,但V2不支持如何将android地图V1转换为android地图V2?,android,google-maps,map,location,Android,Google Maps,Map,Location,我有一个android应用程序,它使用android MapV1。现在我想用一些特殊功能更新应用程序,但当我尝试更新应用程序时,MapV1已被弃用 我想将我的应用程序从MapV1转换为V2,请任何人在这方面帮助我 注意:我在V1中使用覆盖,但V2不支持 public class AlertsMapActivity extends MapActivity { private static final int DIALOG_CONFIRM_REMOVE = 0; private static fin
public class AlertsMapActivity extends MapActivity {
private static final int DIALOG_CONFIRM_REMOVE = 0;
private static final int DIALOG_CONFIRM_SAVE = 1;
private static final double ZOOM_PADDING_FACTOR = 1.2;
public class PopupViewHolder {
public Button buttonDelete,
buttonCreate,
buttonSave;
public EditText editTitle,
editSnippet;
}
private Geocoder geocoder;
private LocationManager locMan;
private DBHelper dbHelper;
private MenuInflater menuInflater;
private MapView mapView;
private View popupView;
private Button buttonClear;
private MapController controller;
private MyLocationOverlay myLocOverlay;
private AlertsOverlay alertsOverlay;
private SearchOverlay searchOverlay;
private RadiusOverlay radiusOverlay;
private ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new DBHelper(this);
menuInflater = getMenuInflater();
locMan = (LocationManager) getSystemService(LOCATION_SERVICE);
geocoder = new Geocoder(this);
G.density = getResources().getDisplayMetrics().density;
// Create popup
popupView = getLayoutInflater().inflate(R.layout.map_popup, null);
Button popupCreate, popupDelete, popupSave;
popupCreate = (Button) popupView.findViewById(R.id.button_create);
popupCreate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GeoPoint point = (GeoPoint) mapView.getTag(R.id.tag_geopoint);
PopupViewHolder popupVH = (PopupViewHolder) popupView.getTag();
String title = popupVH.editTitle.getText().toString(), snippet = popupVH.editSnippet.getText().toString();
Alert alert = new Alert(point, title, snippet, radiusOverlay.getRadius(), C.DEFAULT_EXPIRATION);
// Add to db
long id = dbHelper.addAlert(alert);
// TODO select vibrate/alert tone
// TODO allow user to set expiration
// TODO scheduling
// TODO styling for edittexts in popup
// TODO styling for listalertsactivity
if (id >= 0) {
// Register proximity alert with locman
double lat = point.getLatitudeE6() / 1e6, lon = point.getLongitudeE6() / 1e6;
locMan.addProximityAlert(lat, lon, radiusOverlay.getRadius(), C.DEFAULT_EXPIRATION, Utils.createAlertPI(getApplicationContext(), id));
alertsOverlay.itemsUpdated();
searchOverlay.clear();
mapView.postInvalidate();
mapView.removeView(popupView);
radiusOverlay.clearUI();
Toast.makeText(AlertsMapActivity.this, R.string.alert_added, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AlertsMapActivity.this, R.string.error_saving_alert, Toast.LENGTH_SHORT).show();
}
}
});
popupDelete = (Button) popupView.findViewById(R.id.button_delete);
popupDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_CONFIRM_REMOVE);
}
});
popupSave = (Button) popupView.findViewById(R.id.button_save);
popupSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_CONFIRM_SAVE);
}
});
PopupViewHolder popupVH = new PopupViewHolder();
popupVH.buttonCreate = popupCreate;
popupVH.buttonDelete = popupDelete;
popupVH.buttonSave = popupSave;
popupVH.editTitle = (EditText) popupView.findViewById(R.id.text_title);
popupVH.editSnippet = (EditText) popupView.findViewById(R.id.text_snippet);
popupView.setTag(popupVH);
// Create mapview
setContentView(R.layout.activity_alerts_map);
mapView = (MapView) findViewById(R.id.map);
// Configure mapview
controller = mapView.getController();
controller.setZoom(C.DEFAULT_ZOOM_LEVEL);
mapView.setBuiltInZoomControls(true);
// Show last known location if available
Location loc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc != null) controller.setCenter(location2GeoPoint(loc));
// Overlays
List<Overlay> overlays = mapView.getOverlays();
Drawable alertMarker = getResources().getDrawable(R.drawable.marker_alert);
alertsOverlay = new AlertsOverlay(dbHelper, popupView, alertMarker);
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
Drawable searchMarker = getResources().getDrawable(R.drawable.marker_search);
searchOverlay = new SearchOverlay(popupView, searchMarker);
List<IItemOverlay> itemOverlays = new ArrayList<IItemOverlay>();
itemOverlays.add(alertsOverlay);
itemOverlays.add(searchOverlay);
Drawable radiusMarker = getResources().getDrawable(R.drawable.radius_handle);
radiusOverlay = new RadiusOverlay(this, itemOverlays, radiusMarker);
overlays.add(radiusOverlay);
overlays.add(searchOverlay);
overlays.add(alertsOverlay);
overlays.add(myLocOverlay);
//Clear button
buttonClear = (Button) findViewById(R.id.button_clear);
buttonClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
radiusOverlay.clearUI();
mapView.removeView(popupView);
searchOverlay.clear();
mapView.postInvalidate();
alertsOverlay.itemsUpdated();
}
});
}
@Override
public void onNewIntent(Intent newIntent) {
if (Intent.ACTION_SEARCH.equals(newIntent.getAction())) {
String query = newIntent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(final String query) {
pd = ProgressDialog.show(this, null, getString(R.string.searching));
searchOverlay.clear();
new AsyncTask<String, Void, Boolean>() {
private List<Address> results = null;
@Override
protected Boolean doInBackground(String... params) {
try {
results = geocoder.getFromLocationName(params[0], 5, C.SG_BOUND_SOUTH, C.SG_BOUND_WEST, C.SG_BOUND_NORTH, C.SG_BOUND_EAST);
} catch (IOException e) {
e.printStackTrace();
}
if (results != null && results.size() > 0) {
for (Address result : results)
searchOverlay.addItem(new SearchResult(result));
// Zoom/pan map to fit all results
zoomToFit(searchOverlay);
return true;
}
return false;
}
protected void onPostExecute(Boolean success) {
if (!success) Toast.makeText(AlertsMapActivity.this, R.string.no_results_returned, Toast.LENGTH_LONG).show();
// Hide spinner
if (pd != null) pd.cancel();
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(AlertsMapActivity.this, RecentSearchProvider.AUTHORITY, RecentSearchProvider.MODE);
suggestions.saveRecentQuery(query, results.size() + " result" + ((results.size() == 1) ? "" : "s"));
}
}.execute(query);
}
@Override
protected void onResume() {
super.onResume();
alertsOverlay.itemsUpdated();
myLocOverlay.enableMyLocation();
}
@Override
protected void onPause() {
super.onPause();
myLocOverlay.disableMyLocation();
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_clear:
List<Alert> alerts = dbHelper.getAlerts();
for (Alert alert : alerts)
locMan.removeProximityAlert(Utils.createAlertPI(getApplicationContext(), alert.getId()));
dbHelper.clearData();
alertsOverlay.itemsUpdated();
mapView.postInvalidate();
mapView.removeView(popupView);
return true;
case R.id.menu_test:
Intent intent = new Intent(this, WakeUpActivity.class);
startActivity(intent);
return true;
case R.id.menu_list_alerts:
intent = new Intent(this, ListAlertsActivity.class);
startActivity(intent);
return true;
case R.id.menu_clear_search:
searchOverlay.clear();
mapView.postInvalidate();
return true;
case R.id.menu_search:
onSearchRequested();
return true;
case R.id.menu_zoom_alerts:
if (alertsOverlay.size() > 0) zoomToFit(alertsOverlay);
else Toast.makeText(this, R.string.no_alerts_to_show, Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_options:
intent = new Intent(this, OptionsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DIALOG_CONFIRM_REMOVE:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.confirm_remove).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Alert alertItem = (Alert) mapView.getTag(R.id.tag_item);
locMan.removeProximityAlert(Utils.createAlertPI(getApplicationContext(), alertItem.getId()));
int numRemoved = dbHelper.removeAlert(alertItem.getId());
if (numRemoved > 0) {
alertsOverlay.itemsUpdated();
mapView.postInvalidate();
mapView.removeView(popupView);
radiusOverlay.clearUI();
Toast.makeText(AlertsMapActivity.this, R.string.alert_removed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AlertsMapActivity.this, R.string.error_removing_alert, Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton("Cancel", null);
return builder.show();
case DIALOG_CONFIRM_SAVE:
builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.confirm_save).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PopupViewHolder popupVH = (PopupViewHolder) popupView.getTag();
String title = popupVH.editTitle.getText().toString(), snippet = popupVH.editSnippet.getText().toString();
Alert alertItem = (Alert) mapView.getTag(R.id.tag_item);
Alert newAlert = new Alert(alertItem.getPoint(), title, snippet, radiusOverlay.getRadius(), C.DEFAULT_EXPIRATION);
newAlert.setId(alertItem.getId());
int numUpdated = dbHelper.updateAlert(newAlert);
if (numUpdated > 0) {
alertsOverlay.itemsUpdated();
mapView.postInvalidate();
mapView.removeView(popupView);
radiusOverlay.clearUI();
Toast.makeText(AlertsMapActivity.this, R.string.changes_saved, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AlertsMapActivity.this, R.string.error_saving_changes, Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton("Cancel", null);
return builder.show();
default:
return super.onCreateDialog(id, args);
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private <T extends IItemOverlay> void zoomToFit(T overlay) {
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
List<? extends OverlayItem> items = overlay.getItems();
for (OverlayItem item : items) {
GeoPoint p = item.getPoint();
int lat = p.getLatitudeE6();
int lon = p.getLongitudeE6();
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
}
controller.zoomToSpan((int)(ZOOM_PADDING_FACTOR*overlay.getLatSpanE6()), (int)(ZOOM_PADDING_FACTOR*overlay.getLonSpanE6()));
controller.animateTo(new GeoPoint((maxLat + minLat) / 2, (maxLon + minLon) / 2));
}
/**
* Converts a Location to a GeoPoint
*
* @param loc
* The Location to convert.
* @return A GeoPoint representation of the Location.
*/
private GeoPoint location2GeoPoint(Location loc) {
return new GeoPoint((int) (loc.getLatitude() * 1e6), (int) (loc.getLongitude() * 1e6));
}
公共类AlertsMapActivity扩展了MapActivity{
私有静态最终整型对话框\u确认\u删除=0;
专用静态最终整型对话框\u确认\u保存=1;
专用静态最终双变焦系数=1.2;
公共类PopupViewHolder{
公共按钮删除,
按钮创建,
按钮保存;
公共编辑文本编辑标题,
编辑片段;
}
私人地理编码器;
私人场所经理洛克曼;
私人DBHelper DBHelper;
私人货币单位;
私有地图视图;
私有视图弹出视图;
私人按钮;
专用映射控制器;
私人MyLocationOverlay myLocOverlay;
私人警报Soverlay警报Soverlay;
私有搜索覆盖搜索覆盖;
专用半径覆盖半径覆盖;
私营部门;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dbHelper=新的dbHelper(this);
menuInflater=getMenuInflater();
locMan=(LocationManager)getSystemService(位置服务);
地理编码器=新地理编码器(本);
G.density=getResources().getDisplayMetrics().density;
//创建弹出窗口
popupView=GetLayoutFlater().充气(R.layout.map_弹出,空);
按钮popupCreate、popupDelete、popupSave;
popupCreate=(按钮)popupView.findViewById(R.id.Button\u create);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
GeoPoint point=(GeoPoint)mapView.getTag(R.id.tag_GeoPoint);
PopupViewHolder popupVH=(PopupViewHolder)popupView.getTag();
字符串title=popupVH.editTitle.getText().toString(),snippet=popupVH.editSnippet.getText().toString();
警报警报=新警报(点、标题、代码段、radiusOverlay.getRadius()、C.DEFAULT\u过期);
//添加到数据库
long id=dbHelper.addAlert(警报);
//TODO选择振动/提示音
//TODO允许用户设置过期时间
//TODO调度
//弹出窗口中编辑文本的TODO样式
//listalertsactivity的TODO样式
如果(id>=0){
//向locman登记近距离警报
双lat=point.getLatitudeE6()/1e6,lon=point.getLongitudeE6()/1e6;
locMan.addProximityAlert(lat,lon,radiusOverlay.getRadius(),C.DEFAULT_EXPIRATION,Utils.createAlertPI(getApplicationContext(),id));
alertsOverlay.itemsUpdated();
searchOverlay.clear();
mapView.postInvalidate();
mapView.removeView(popupView);
radiusOverlay.clearUI();
Toast.makeText(AlertsMapActivity.this,R.string.alert_added,Toast.LENGTH_SHORT).show();
}否则{
Toast.makeText(AlertsMapActivity.this,R.string.error_saving_alert,Toast.LENGTH_SHORT).show();
}
}
});
popupDelete=(按钮)popupView.findviewbyd(R.id.Button\u delete);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
showDialog(对话框\u确认\u删除);
}
});
popupSave=(按钮)popupView.findViewById(R.id.Button\u save);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
showDialog(对话框确认保存);
}
});
PopupViewHolder popupVH=新的PopupViewHolder();
popupVH.buttonCreate=popupCreate;
popupVH.buttonDelete=popupDelete;
popupVH.buttonSave=popupSave;
popupVH.editTitle=(EditText)popupView.findViewById(R.id.text\u title);
popupVH.editSnippet=(EditText)popupView.findViewById(R.id.text\u snippet);
设置标签(popupVH);
//创建地图视图
setContentView(R.layout.activity\u alerts\u map);
mapView=(mapView)findViewById(R.id.map);
//配置地图视图
controller=mapView.getController();
controller.setZoom(C.DEFAULT\u ZOOM\u LEVEL);
mapView.SetBuilTinZoomControl(真);
//显示最后一个已知位置(如果可用)
Location loc=locMan.getlastnownlocation(LocationManager.NETWORK\u PROVIDER);
if(loc!=null)controller.setCenter(location2GeoPoint(loc));
//覆盖层
List overlays=mapView.getOverlays();
Drawable alertMarker=getResources().getDrawable(R.Drawable.marker\u alert);
alertsOverlay=新的alertsOverlay(dbHelper、popupView、alertMarker);
myLocOverlay=新的MyLocationOverlay(这是地图视图);
myLocOverlay.enableMyLocation();
Drawable searchMarker=getResources().getDrawable(R.Drawable.marker\u search);
searchOverlay=新的searchOverlay(弹出视图,搜索标记);
List itemOverlays=新的ArrayList();
itemOverlays.add(alertsOverlay);
itemOverlays.add(searchOverlays);
Drawable radiusMarker=getResources().getDrawable(R.Drawable.radius\u句柄);
radiusOverlay=新的radiusOverlay(此,itemOverlays,radiusMarker);
叠加。添加(半径叠加);
覆盖。添加(搜索覆盖);
叠加。添加(alertsOverlay);
叠加。添加(myLocOverlay);
//清除按钮
buttonClear=(按钮)findViewById(R.id.Button\u clear);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
radiusOverlay.clearUI();
mapView.removeView(popupView);
searchOverlay.clear();
mapView.postInvalidate();
alertsOverlay.itemsUpdated();
}
});
}
@凌驾
公共帐篷(新意向){
我