0

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

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

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 地图中获得了搜索位置。我如何提供对话框。当我们单击地图位置时?

提前致谢......

4

2 回答 2

0

您可以为此使用弹出窗口。弹出窗口可以显示在屏幕上的任何位置。只需注意单击的坐标并在该位置显示弹出窗口。

以下是相同的一般示例。

为您的弹出窗口创建布局

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。使用适配器以与通常相同的方式填充列表视图。

于 2012-05-03T06:35:44.767 回答
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();
    }
}
}
于 2012-05-03T06:46:27.320 回答