2012-10-26 36 views
0

我需要在自定義地圖應用上實現搜索功能,就像在本地谷歌地圖中一樣(操作欄變成搜索字段,並且可以編寫查詢) 。現在我知道如何使用google geocoding api,以及如何從數據中檢索位置。但是我沒能實現那個可變動的操作欄。在自定義應用中實現地圖搜索功能(如在本地谷歌地圖中)

我的應用程序看起來像這樣:

enter image description here

後,我按下搜索按鈕,我想有這種佈局顯示:

enter image description here

感謝您的幫助,希望你能解決我的問題。

+1

這個問題是不是在谷歌地圖API V3。 (標籤已移除)。 – Marcelo

回答

2

以下是您的搜索工具的一些代碼。這段代碼和我一起工作。如果您輸入地點名稱,則會將您在地圖上重定向到完全匹配的地點。下面的代碼提供了兩種搜索方式,首先由名字命名,然後由LatLang命名。

  1. 通過名稱OG位置

    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. 通過LAtLang。

    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(); 
    
    } 
    
+0

嗨,謝謝你的答案,它很酷。但主要問題並沒有解決。我需要像在標準谷歌地圖中那樣更改操作欄。我已經提出了你的問題,但我不能接受。 – Csabi