2012-01-30 129 views
0

我正在創建一個應用程序,它在地圖上搜索地址並將地點作爲地圖上的標記返回。地圖覆蓋android和搜索地點

這是我的代碼

package org.gmaps.com; 



import java.io.IOException; 
import java.util.List; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Overlay; 

public class GmapsActivity extends MapActivity { 
    MapView mapView; 
    Geocoder geoCoder; 
    MapController mc; 
    GeoPoint q; 




    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mapView=(MapView)findViewById(R.id.mapView); 
     // LinearLayout zoom=(LinearLayout)findViewById(R.id.zoom); 
     mapView.setBuiltInZoomControls(true); 
     double lat=(8.47662*1E6); 
     double lon=(76.9441*1E6); 
     GeoPoint p = new GeoPoint ((int)lat,(int) lon); 
     final MapController mc = mapView.getController(); 
     mc.animateTo(p); 
     int zoomlevel = mapView.getMaxZoomLevel(); // detect maximum zoom level 
     mc.setZoom(zoomlevel - 5); // zoom 
     mapView.setSatellite(false); 
     Button bt=(Button)findViewById(R.id.bt); 
     bt.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       EditText txtsearch=(EditText)findViewById(R.id.et); 



       List<Address> addresses = null; 
       try { 
        addresses = geoCoder.getFromLocationName(txtsearch.getText().toString(),5); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       if(addresses.size() > 0) 
       { 
        q = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), 
             (int) (addresses.get(0).getLongitude() * 1E6)); 

         mc.animateTo(q); 
         mc.setZoom(12); 

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

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

     }); 



    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     mc = mapView.getController(); 
     switch (keyCode) 
     { 
      case KeyEvent.KEYCODE_3: 
       mc.zoomIn(); 
       break; 
      case KeyEvent.KEYCODE_1: 
       mc.zoomOut(); 
       break; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
} 
class MapOverlay extends com.google.android.maps.Overlay 
{private Context context; 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow); 
     GmapsActivity ob=new GmapsActivity(); 

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

     //---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-32, null);   
     return true; 
    } 
} 

當我點擊搜索按鈕,我得到的錯誤..

空指針異常..

我已經使用了應答此頁面上

How can I find the latitude and longitude from address?

我得到了錯誤getfromlocationname並且必須拋出異常。

我已閱讀上述鏈接上的評論,說很多人已經得到了它與在該提供的答案工作.. 我在哪裏出錯了?

請指導。

回答

0

你的EditText txtSearch可能會是零,而導致空指針異常

試試這個:

private EditText et; // Create a private Variable for your EditText (as a class variable) 

比在onCreate方法

et = (EditText)findViewById(R.id.et); 

現在你可以使用一個叫做你的EditText 'et'獲取文本

Button bt=(Button)findViewById(R.id.bt); 
bt.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     //EditText txtsearch=(EditText)findViewById(R.id.et); -> WRONG 

     String txt = ""; 
     if(et != null) 
      txt = et.getText().toString(); 

     List<Address> addresses = null; 
     try { 
      addresses = geoCoder.getFromLocationName(txt,5); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     ..... etc 
+0

no.it不是疑難問題用吐司我得到的文本已經測試。我收到這些錯誤。 01-31 10:39:09.627:E/AndroidRuntime(454):java.lang.IllegalStateException:無法執行活動的方法01-31 10:39:09.627:E/AndroidRuntime(454):\t at android.view .View $ 1.onClick(View.java:2144)01-31 10:39:09.627:E/AndroidRuntime(454):導致:java.lang.reflect.InvocationTargetException 01-31 10:39:09.627:E/AndroidRuntime (454):\t at java.lang.reflect.Method.invokeNative(Native Method – Maddy 2012-01-31 05:11:26

0

你忘了初始化地理編碼器。

不要在您的onCreate:

geoCoder = new Geocoder(this); 

geoCoder = new Geocoder(this,Locale.getDefault());