1

我在 Android 程序中显示谷歌地图。当我点击地图中的一个地方时,如何将这些地方保存到 Android Listview。

通过这个,我正在地图中获取搜索位置

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 SampleMpas 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(SampleMpas.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(SampleMpas.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(SampleMpas.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(15);
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(SampleMpas.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
        SampleMpas.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);

}

@Override
protected Dialog onCreateDialog(int id) {

    // TODO Auto-generated method stub

    switch (id) {
        case 0:
            return new AlertDialog.Builder(this).setTitle("Are You Want save this place As?").setIcon(
                    R.drawable.icon).setPositiveButton("Favorite",
                    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

                        }
                    }).setCancelable(true).setNeutralButton("Business", 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;
        }



@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);
        double lat = 0;
        double lang = 0;
        ListofGeopoints.add(new OverlayItem(getPoint(lat, lang),
                "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();
    }
}
}

提前致谢。

4

1 回答 1

0

您可以通过以下方式获取触摸位置的地理点

  @Override
public boolean onTap(GeoPoint p , MapView mapView) 

编写一个派生自 Overlay 类的类并覆盖 onTap() 方法。然后,您可以将叠加层添加到您的 MapView。当您在地图上的某个位置使用 Tab 时,代表您点击位置的 GeoPoint 对象将传递给 onTap() 方法。将此地理点保存在您的数据库中并从数据库中填充您的列表视图。

示例代码:覆盖类:

class MapOverlay extends
        com.google.android.maps.ItemizedOverlay<OverlayItem> {

    @Override
    public boolean onTap(GeoPoint p , MapView mapView) {

        // TODO Auto-generated method stub
    Log.i("Latitude" , String.valueOf(p.getLatitudeE6()/1E6));
    Log.i("Longitude", String.valueOf(p.getLongitudeE6()/1E6));


        return super.onTap(p, mapView);
    }
于 2012-05-07T06:48:09.600 回答