【问题标题】:Implement map search function on custom Application like in native google maps在自定义应用程序上实现地图搜索功能,如原生谷歌地图
【发布时间】:2012-10-26 06:36:19
【问题描述】:

我需要在我的自定义地图应用程序上实现搜索功能,就像在原生谷歌地图中完成的那样(操作栏变成搜索字段,您可以编写查询)。现在我知道如何使用google geocoding api,以及如何从数据中检索位置。但我未能实现那个可变的操作栏。

我的应用是这样的:

在我按下搜索按钮后,我希望显示这种布局:

感谢您的帮助,希望您能解决我的问题。

【问题讨论】:

  • 这个问题与 Google Maps API V3 无关。 (已删除标签)。

标签: android google-maps android-searchmanager


【解决方案1】:

这是您的搜索工具的一些代码。这段代码对我有用。如果您输入位置名称,这会将您在地图上重定向到完全匹配的位置。下面的代码提供了两种搜索方式,第一种是 Name,第二种是 LatLang。

  1. 按名称和位置

    public void searchPlace()
    {       
    
        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);    // create mapController object like `MapController mc = mapView.getController();`
                    mapView.invalidate();
                }    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
          }
        });
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });
    
        alert.show();
    
    }  
    
  2. 拉特朗。

    public void byLatLang()
    {       
    
        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.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();
    
    }  
    

【讨论】:

  • 您好,感谢您的回答,它很酷。但主要问题并没有解决。我需要像在标准谷歌地图中一样更改操作栏。我赞成你的问题,但我不能接受。
猜你喜欢
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多