2

我想在 Android 中显示谷歌地图基于用户编辑文本值 lat 和 lang 和地名,然后我最后点击搜索按钮,地图显示在 Android 中。

4

2 回答 2

3

你需要创建一些像这样的对象。

MapView mapView; 
MapController mc;
GeoPoint p;
String add = "";
double lattitude ;
double longitude;

按名称搜索位置并显示地图。

 AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Search Location");
        alert.setMessage("Enter Location Name: ");

        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value = input.getText().toString();
          // Do something with value!
          Log.d("value", value);

          Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());    
            try {
                List<Address> addresses = geoCoder.getFromLocationName(
                    value, 5);
                String add = "";
                if (addresses.size() > 0) {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));
                    mc.animateTo(p);    
                    mapView.invalidate();
                }    
            } catch (IOException e) {
                e.printStackTrace();
            }


          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });

        alert.show();

    }

通过输入纬度和经度搜索位置并显示地图..

    searchUsingLangLat()
    {       

        LayoutInflater factory = LayoutInflater.from(this);            
        final View textEntryView = factory.inflate(R.layout.latlong, null);

        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Search Location");
        alert.setMessage("Enter Lattitude and Longitude: ");

        alert.setView(textEntryView); 
        // Set an EditText view to get user input
        AlertDialog latLongPrompt = alert.create();

        final EditText lat = (EditText) textEntryView.findViewById(R.id.lat);
        final EditText longi = (EditText) textEntryView.findViewById(R.id.longi);
    //alert.setView(lat);
        //alert.setView(longi);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            Toast.makeText(getBaseContext(), "clicked ok ", Toast.LENGTH_SHORT).show();
          Double value1 = Double.parseDouble(lat.getText().toString());
          Double value2 = Double.parseDouble(longi.getText().toString());
          // Do something with value!
         // Log.d("value1", value1);
          //Log.d("value2", value2);

          p = new GeoPoint(
                    (int) (value1 * 1E6), 
                    (int) (value2 * 1E6));

                mc.animateTo(p);
                mc.setZoom(17); 
                mapView.invalidate();


          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });

        alert.show();

最后你需要显示地图。在 onCreate() 方法中编写以下代码。

mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {"1.352566007", "103.78921587"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lng * 1E6));

    mc.setCenter(p);
    mc.setZoom(17); 

    //---Add a location marker---
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);      


    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

试试这个代码......如果你有任何错误可以免费询问。

于 2012-05-05T12:31:27.837 回答
0

如果您将一堆位置的纬度和经度存储在 SQLite 数据库中,您可以检索这些值并将它们分别放在一个 OverlayItem 类中,以便在 Google 的地图代码中使用!!

  1. 数据库名称:数据库
  2. 表名:地点

字段到位表:

  1. id 标题 描述 纬度 经度

你会想做这样的查询:

SELECT title, description, latitude, longitude
FROM place

这可以像这样在Android中完成:

    // get your database object here using your DbHelper object and the
    // method getReadableDatabase();
    ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
    Cursor locationCursor = databaseObject.query("place", new String[] {
            "title", "description", "latitude", "longitude" }, null, null,
            null, null, null);

    locationCursor.moveToFirst();
    do {
        String title = locationCursor.String(locationCursor
                .getColumnIndex("title"));
        String description = locationCursor.String(locationCursor
                .getColumnIndex("description"));
        int latitude = (int) (locationCursor.getDouble(locationCursor
                .getColumnIndex("latitude")) * 1E6);
        int longitude = (int) (locationCursor.getDouble(locationCursor
                .getColumnIndex("longitude")) * 1E6);

        items.add(new OverlayItem(new GeoPoint(latitude, longitude), title,
                description));
    } while (locationCursor.moveToNext());

您需要将双精度值乘以 1e6,因为 Android 使用 lat/long 值的整数表示。如果您在填充数据库时已经考虑到这一点,请跳过乘法并使用 locationCursor.getInt(...)。

于 2012-05-05T12:36:36.427 回答