Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

How to implement google maps search by address in Android?

How to implement google maps search by address in Android?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For that you need to create one map overlay class in map activity.

Example

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

Where please change R.drawable.pink . Put your any drawable.

Now please write below code in your search onClick event..

Example

List<Address> addresses = geoCoder.getFromLocationName(txtsearch.getText().toString(),5);

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 = map.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);

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

Where txtsearch is a EditText and controller is a map controller ..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...