2012-02-21 83 views
0

我想,當用戶擡起手指代碼通過像素跟蹤lat和多頭並觸發一個地址解析請求我的代碼,活動更新顯示在屏幕即每個位置的用戶觸摸位置的名字是地理編碼錯誤

package scheduler.scheduler; 
import java.util.List; 
import com.google.android.maps.MapView.LayoutParams; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.app.Activity; 
import android.os.Bundle; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController; 
import com.google.android.maps.Overlay; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.LinearLayout; 
import java.io.IOException; 
import android.view.MotionEvent; 
import android.location.Geocoder; 
import java.util.Locale; 
import android.widget.Toast; 
import android.location.Address; 
public class mapz extends MapActivity { 
    /** Called when the activity is first created. */ 
MapView mapView; 
MapController mc; 
GeoPoint p; 
class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 
     //super.draw(canvas, mapView, shadow);     

     //---translate the GeoPoint to screen pixels--- 
     //Point screenPts = new Point(); 
     //mapView.getProjection().toPixels(p, screenPts); 

     //---add the marker--- 
     //Bitmap bmp = BitmapFactory.decodeResource(
      //getResources(), R.drawable.pushpin);    
     //canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
     return true; 
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    { 
     //---when user lifts his finger--- 
     if (event.getAction() == 1) {     
      GeoPoint p = mapView.getProjection().fromPixels(
       (int) event.getX(), 
       (int) event.getY()); 
      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"; 
        } 

        Toast.makeText(getBaseContext(), "hii", Toast.LENGTH_SHORT).show(); 
       } 
       catch (IOException e) {     
        e.printStackTrace(); 
       } 

       return true; 
     } 
      else     
       return false; 
    } 
} 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.maplayout); 
     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[] = {"18.116667", "83.416667"}; 
     double lat = Double.parseDouble(coordinates[0]); 
     double lng = Double.parseDouble(coordinates[1]); 

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

     mc.animateTo(p); 

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

    } 


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

活動加載和地圖加載,但是當我點擊並釋放鼠標預期的行動,即「應顯示地點的地址」不工作當我檢查的logcat它顯示

enter image description here

任何專家,請TR Ÿ幫助我,我必須快速解決它,我的項目工作!

+0

確保艙單的權限是正確的<使用許可權的android:NAME =「機器人.permission.ACCESS_FINE_LOCATION」 /> \t <用途的許可安卓:名稱= 「android.permission.ACCESS_COARSE_LOCATION」/> \t <使用許可權的android:name = 「android.permission.INTERNET對」/> – 2012-02-21 18:18:11

+0

也提升項目級別到谷歌api 2.2或更新比 – 2012-02-21 18:19:48

+0

項目已經在谷歌API 2.2我改變了清單 仍然是一樣的錯誤! – 2012-02-21 18:32:18

回答