2011-11-22 70 views
0

我有這個文本框中的其他活動和一個導致這張地圖的按鈕。我想要做的是,它允許用戶點擊地圖並指向他們想要的位置,並且他們還可以將該位置的地址提取回文本框而不用鍵入。它可以這樣做嗎?從現在開始,這個警告對話框會在用戶引腳指向2個按鈕(是和否)之後彈出。如果是,則將該位置地址提取回文本框。任何想法?在地圖上的用戶針點位置,並獲取該位置的地址回到文本框

static EditText txtLocation; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mapview); 

     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mc = mapView.getController(); 

     List<Overlay> mapOverlays = mapView.getOverlays(); 
     MapOverlay mapOverlay = new MapOverlay(); 

     mapOverlays.add(mapOverlay); 

     // obtain gps location 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     locationListener = new MyLocationListener(); 

     lm.requestLocationUpdates(
     // LocationManager.GPS_PROVIDER, 
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } 

    private class MyLocationListener implements LocationListener { 

     public void onLocationChanged(Location loc) { 
      if (loc != null) { 
       Toast.makeText(
         getBaseContext(), 
         "Location changed: Lat: " + loc.getLatitude() 
           + " Lng: " + loc.getLongitude(), 
         Toast.LENGTH_SHORT).show(); 
      } 
      p = new GeoPoint((int) (loc.getLatitude() * 1E6), 
        (int) (loc.getLongitude() * 1E6)); 
      mc.animateTo(p); 
      mc.setZoom(18); 

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

      // invalidate() method forces the MapView to be redrawn 
      mapView.invalidate(); 
     } 

     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    class MapOverlay extends com.google.android.maps.Overlay { 

     @Override 
     public boolean onTap(final GeoPoint p, MapView mapView) { 
      // TODO Auto-generated method stub 

      k = p; 
      mc = mapView.getController(); 
      mc.animateTo(p); 

      mapView.invalidate(); 

      Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); 
      try{ 
      List<Address> addresses = geoCoder.getFromLocation(
      p.getLatitudeE6() /1E6, 
      p.getLongitudeE6()/1E6, 1 
      ); 
      String add = ""; 
      if (addresses.size()>0) 
      { 
       for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) 
        add += addresses.get(0).getAddressLine(i) + "\n"; 
      } 

//   txtLocation.setText(add); 
      Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); 
      } 
      catch (IOException e){ 
       e.printStackTrace(); 
      } 

      new AlertDialog.Builder(MapsActivity.this) 
        .setTitle("Change location..") 
        .setMessage("go to the new location?") 
        .setNegativeButton("NO", 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // TODO Auto-generated method stub 
            dialog.dismiss(); 
           } 
          }) 
        .setPositiveButton("YES", 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // TODO Auto-generated method stub 
            Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); 
            try{ 
            List<Address> addresses = geoCoder.getFromLocation(
            p.getLatitudeE6() /1E6, 
            p.getLongitudeE6()/1E6, 1 
            ); 
            String add = ""; 
            if (addresses.size()>0) 
            { 
             for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) 
              add += addresses.get(0).getAddressLine(i) + "\n"; 
            } 

            txtLocation.setText(add); 
//         Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); 
            } 
            catch (IOException e){ 
             e.printStackTrace(); 
            } 
           } 
          }).show(); 
      return true; 
     } 

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
       long when) { 
      super.draw(canvas, mapView, shadow); 
      if (k != null) { 
       // ---translate the GeoPoint to screen pixels--- 
       Point screenPts = new Point(); 
       mapView.getProjection().toPixels(k, screenPts); 

       // ---add the marker--- 
       Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
         R.drawable.marker); 
       canvas.drawBitmap(bmp, screenPts.x - 10, screenPts.y - 34, null); 
      } 
      return true; 
     } 
    } 
} 
+0

請清楚你的問題...你想獲取地址的觸摸位置或當前位置? 刪除onTap中的註釋代碼,üshud傳遞這些經緯度值,用於反向地理編碼... –

+0

我想將地址提取到用戶所在位置的文本框中。 – sowhat

+0

在onTap函數中使用textbox.setText(add) –

回答

1

那麼你可以嘗試反向地理編碼。您需要的是提供用戶點擊的長點,這相當容易。
檢查this類似的問題。可能有幫助。

+0

我試過反向地理編碼,我把代碼放在按鈕是的。 – sowhat

+0

它沒有返回地址? – Urban

+0

之後我點擊是的。它會突出顯示此特定行:\t \t \t \t .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); – sowhat