2012-12-15 114 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){..”