How to implement google maps search by address in Android?
You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).
For that you need to create one map overlay class in map activity.
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..
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 ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With