Google Maps Search by Address in Android

Devendar picture Devendar · Dec 15, 2012 · Viewed 7.4k times · Source

For Implementing Google Maps Search by Address

i followed the below Answer in stackoverflow

How to implement google maps search by address

i Tried almost 2 days and searched for solutions not found.

Here i am adding My code

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 (Main Activity class)

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 - the error i got in my LogCat - please go through this

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

please help me

Answer

iagreen picture iagreen · Dec 15, 2012

The log message --

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

Clues you in that you appear to be running on a device without Google Maps API on it. If it is an emulator go to your AVD manager and create a new AVD with a target that has "Google APIs (Google Inc.) -- API Level X" where X is your target API.

If this is a real device, it looks like you have one without google maps support, try in the emulator.