3

用于按地址实施 Google 地图搜索

我在stackoverflow中遵循了以下答案

如何实现谷歌地图按地址搜索

我尝试了将近 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

请帮我

4

2 回答 2

2

日志消息——

“由:java.lang.ClassNotFoundException:加载程序dalvik.system.PathClassLoader [/data/app/com.example.searchmap-2.apk]中的com.google.android.maps.MapView引起

暗示您似乎在没有 Google Maps API 的设备上运行。如果它是模拟器,请转到您的 AVD 管理器并创建一个新的 AVD,其目标为“Google APIs (Google Inc.) -- API Level X”,其中 X 是您的目标 API。

如果这是一个真实的设备,看起来你有一个不支持谷歌地图的设备,请在模拟器中尝试。

于 2012-12-15T02:43:01.030 回答
0

引起:java.lang.ClassNotFoundException: com.google.android.maps.MapView in loader dalvik.system.PathClassLoader[/data/app/com.example.searchmap-2.apk]

这里data指的是 SD 卡。如果 SD 卡已从设备中移除,并且应用程序存储在其上,则打开应用程序会如上所述崩溃,因为设备无法找到 APK。我可以在 HTC 感觉版本 2.3.4 再现。

于 2013-06-28T09:04:20.790 回答