显示基于地图的警告对话框点击?

时间:2012-05-03 06:16:49

标签: android listview maps alertdialog

我正在练习Android地图现在我在我的模拟器中成功地获取地图当我在谷歌市场搜索时,我发现一个有趣的应用程序我试图开发示例应用程序

https://play.google.com/store/apps/details?id=streetdirectory.mobile&feature=search_result#?t=W251bGwsMSwxLDEsInN0cmVldGRpcmVjdG9yeS5tb2JpbGUiXQ ..

package com.example.tutorials;

import java.io.IOException;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;

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.MapView.LayoutParams;  
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class GoogleMap extends MapActivity  
{

     MapView mapView; 

    /** Called when the activity is first created. */
    @Override
    protected boolean isRouteDisplayed() 
    {
    return false;
    }

    public void changeMap(String area)
    {

    mapView = (MapView) findViewById(R.id.mapview);
    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(GoogleMap.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(GoogleMap.this, "record not found", Toast.LENGTH_SHORT).show();
    return;
    }
    }
    catch(IOException io)
    {
    Toast.makeText(GoogleMap.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
        public void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnSearch=(Button) findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


        EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
        String area=txtSearch.getText().toString();
        Toast.makeText(GoogleMap.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
        GoogleMap.this.changeMap(area);


                    }
        });

        mapView = (MapView) findViewById(R.id.mapview);
        MapController mapController = mapView.getController();
        mapController.setZoom(14);

        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 

        }

    }

通过这段代码我在Android maps中获得搜索位置。我可以提供对话框。当我们点击地图的地方吗?

先谢谢......

2 个答案:

答案 0 :(得分:0)

您可以使用popup .Popup可以显示在屏幕上的任何位置。请注意点击和显示该地点的弹出窗口。

下面是相同的一般例子。

为弹出窗口

创建布局
private PopupWindow mPopup;
//Some code here
LayoutInflater inflater = (LayoutInflater) 
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //Inflate the view from a predefined XML layout
    //This is how your popup will look like
    View layout = inflater.inflate(R.layout.popup_layout,
            (ViewGroup) findViewById(R.id.popup_element));

    mPopup = new PopupWindow(layout, "width", "height", true);
    //Use below when you want to display at specific x-y coord on screen
    mPopup .showAtLocation(layout, Gravity.CENTER, "x-coord", "y-coord");


    //You can use some thing like below to get views from your layout
    TextView mText = (TextView) layout.findViewById(R.id."id for textview");
    mText.setText("Just an example you can set buttons and listviews in similar fashion");

    mPopup.dismiss();//When you want to dismiss Popup

你想拥有listView所以你的布局xml将有一个listview。使用适配器以正常方式填充列表视图。

答案 1 :(得分:0)

public class MapViewer extends MapActivity {

MapView mapView;

MapController mapController;

GeoPoint mgeoPoint;

Drawable marker;

MyLocationOverlay mLocationOverlay;

MotionEvent e;


@Override
protected void onCreate(Bundle icicle) {

    // TODO Auto-generated method stub
    super.onCreate(icicle);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapView);

    mapController = mapView.getController();

    mapView.setBuiltInZoomControls(true);

    mapView.setStreetView(true);


    marker = getResources().getDrawable(R.drawable.pushpin);

    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("Hello").setIcon(
                    R.drawable.ic_launcher).setPositiveButton("Yes",
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog , int which) {

                            // TODO Auto-generated method stub

                        }
                    }).setCancelable(true).setNegativeButton("Cancel",
                    new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog , int which) {

                            // TODO Auto-generated method stub

                        }
                    })

            .setOnCancelListener(new OnCancelListener() {

                @Override
                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();
    }
}
}