2013-06-18 7 views
9

Jestem początkującym programistą Androida. Opracowuję aplikację Map. Mam funkcję wyszukiwania adresu, ale nie wiem, jak wyszukiwać adres według nazwy za pomocą interfejsu Google Map API V.2. Proszę, zasugeruj mi rozwiązanie i kod do rozwiązania tego problemu. Dziękuję bardzo i przepraszam z moim angielskim.jak wyszukiwać adres według nazwy na Google Map Android

+0

http://stackoverflow.com/questions/5375654/how-to-implement-google-maps-search-by-address-in-android – Dimmerg

Odpowiedz

5
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search_map); 

     editText = (EditText) findViewById(R.id.editText1); 
     mapView = (MapView) findViewById(R.id.mapView); 
     btngo = (Button) findViewById(R.id.btnmapsites); 

      btngo.setOnClickListener(new View.OnClickListener() {        

      public void onClick(View arg0) { 


     List<Address> addresses; 
     try { 
      addresses = geoCoder.getFromLocationName(editText.getText().toString(),1); 
      if(addresses.size() > 0) 
      { 
       p = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), 
            (int) (addresses.get(0).getLongitude() * 1E6)); 

        controller.animateTo(p); 
        controller.setZoom(12); 

        MapOverlay mapOverlay = new MapOverlay(); 
       List<Overlay> listOfOverlays = mapView.getOverlays(); 
         listOfOverlays.clear(); 
       listOfOverlays.add(mapOverlay); 

        mapView.invalidate(); 
        editText.setText(""); 
      } 
      else 
      { 
        AlertDialog.Builder adb = new AlertDialog.Builder(SearchMapActivity.this); 
        adb.setTitle("Google Map"); 
        adb.setMessage("Please Provide the Proper Place"); 
        adb.setPositiveButton("Close",null); 
        adb.show(); 
      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      } 
      }); 


    } 
+1

Dziękuję Segi ale jeśli jest to możliwe unikaj Geopoint, ponieważ myślę, że w Google Map V2 nie ma Geopoing. – shiteru

21
adderess = c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT3)); 
// get address in string for used location for the map 

/* get latitude and longitude from the adderress */ 

Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); 
try 
{ 
    List<Address> addresses = geoCoder.getFromLocationName(adderess, 5); 
    if (addresses.size() > 0) 
    { 
     Double lat = (double) (addresses.get(0).getLatitude()); 
     Double lon = (double) (addresses.get(0).getLongitude()); 

     Log.d("lat-long", "" + lat + "......." + lon); 
     final LatLng user = new LatLng(lat, lon); 
     /*used marker for show the location */ 
     Marker hamburg = map.addMarker(new MarkerOptions() 
      .position(user) 
      .title(adderess) 
      .icon(BitmapDescriptorFactory 
      .fromResource(R.drawable.marker))); 
     // Move the camera instantly to hamburg with a zoom of 15. 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(user, 15)); 

     // Zoom in, animating the camera. 
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
    } 
} 
catch (IOException e) 
{ 
    e.printStackTrace(); 
} 
+0

Thx Haresh, Działa. :) – shiteru