2012-12-15 145 views
3

地址爲通過地址谷歌地圖在Android的

實現谷歌地圖搜索我跟着計算器下面的答案

How to implement google maps search by address

我幾乎嘗試了2天,搜索沒有找到解決方案。

我在這裏添加我的代碼

activity_search_map.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#3b3b3b"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:background="#2a2a2a" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="450dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

    <Button 
     android:id="@+id/btnmapsites" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:text="Go" 
     android:padding="15dip" android:layout_weight="1" 
     android:gravity="center"/> 

    </LinearLayout> 



<com.google.android.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:apiKey="0Hy-jVRgYeXOLNcLWl8N7bFl4spAl7s4SX4cYSg" 
/> 

</LinearLayout> 

SearchMapActivity.java(主Activity類)

package com.example.searchmap; 

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

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

import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class SearchMapActivity extends Activity { 

    Geocoder geoCoder; 
    EditText editText; 
    GeoPoint p; 
    MapController controller; 
    MapView mapView; 
    Button btngo; 

    @Override 
    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(); 
     } 
      } 
      }); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_search_map, menu); 
     return true; 
    } 

} 

MapOverlay.java

package com.example.searchmap; 

import java.util.List; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 

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.app.Activity; 
import android.content.res.Resources; 
import android.view.Menu; 
import android.widget.EditText; 

class MapOverlay extends com.google.android.maps.Overlay 
{ 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 
    GeoPoint p = null; 
      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.red);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-32, null);   
     return true; 
    } 

    private Resources getResources() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

logcat的 - 我在logcat中得到了錯誤 - 請去通過這個

12-14 19:46:46.076: E/AndroidRuntime(1043): FATAL EXCEPTION: main 
12-14 19:46:46.076: E/AndroidRuntime(1043): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.searchmap/com.example.searchmap.SearchMapActivity}: android.view.InflateException: Binary XML file line #35: Error inflating class com.google.android.maps.MapView 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.os.Looper.loop(Looper.java:130) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at java.lang.reflect.Method.invoke(Method.java:507) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at dalvik.system.NativeStart.main(Native Method) 
12-14 19:46:46.076: E/AndroidRuntime(1043): Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class com.google.android.maps.MapView 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.Activity.setContentView(Activity.java:1657) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at com.example.searchmap.SearchMapActivity.onCreate(SearchMapActivity.java:38) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  ... 11 more 
12-14 19:46:46.076: E/AndroidRuntime(1043): Caused by: java.lang.ClassNotFoundException: com.google.android.maps.MapView in loader dalvik.system.PathClassLoader[/data/app/com.example.searchmap-2.apk] 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.createView(LayoutInflater.java:471) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570) 
12-14 19:46:46.076: E/AndroidRuntime(1043):  ... 20 more 

請幫我

+0

主活動類無法獲取你的地址。現在你的嘗試導致同步衝突。嘗試獲取地址公式(即「...列出

地址; 嘗試{....」)出主線程。要獲得正確的地址,請參閱[本示例教程](http://wptrafficanalyzer.in/blog/android-geocoding-showing-user-input-location-on-google-map-android-api-v2/ )。 – BBonDoo

回答

2

日誌消息 -

「產生的原因:拋出java.lang.ClassNotFoundException: com.google.android.maps.MapView裝載機 dalvik.system.PathClassLoader [/data/app/com.example.searchmap-2.apk]

提示您看起來是在沒有Google Maps API的設備上運行的。如果它是仿真器,請轉至您的AVD管理器,並創建一個新的AVD,其目標爲「Google API(Google Inc.) - API Level X」,其中X是您的目標API。

如果這是一個真正的設備,它看起來像你有一個不支持谷歌地圖,嘗試在模擬器。

0

引起:拋出java.lang.ClassNotFoundException:com.google.android.maps.MapView在裝載機dalvik.system.PathClassLoader [/data/app/com.example.searchmap-2.apk]

這裏data表示SD卡。如果SD卡已從設備中移除,並且該應用程序已存儲在該設備上,則打開該應用程序會如上所述崩潰,因爲該設備無法找到APK。我可以在HTC感覺版本2.3.4中重現。因爲你試圖讓內的地址「公共無效的onClick(查看爲arg0){..」