How to implement google maps search by address in Android?
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 ..
In Android Google Map, how do I search address within specific city?
add this line for city search
&components=locality:ahmedabad
search address within specific city
http://maps.googleapis.com/maps/api/geocode/json?address=Singapore%20505468&sensor=true&components=locality:ahmedabad
Google Maps Search by Address in Android
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.
Enabling search feature in google maps using Android Studio
Instead of implementing a SearchView, add the places API from google in your grade file as follows:
implementation 'com.google.android.libraries.places:places:2.3.0'
and use AutocompleteSupportFragment as follows:
try {
if (!Places.isInitialized()) {
Places.initialize(getActivity().getApplicationContext(), GlobalVariables.google_api_key);
}
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.getView().setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bginfo_whit));
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
autocompleteFragment.setCountry("ET");
autocompleteFragment.setMenuVisibility(false);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
LatLng newLatLng = place.getLatLng();
mMap.moveCamera(CameraUpdateFactory.newLatLng(newLatLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
//Log.i(TAG, "An error occurred: " + status);
}
});
}
catch (Exception e)
{}
open google maps through intent for specific location in android
I have not tested this but you could try :
First method:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
EDIT:
This might not work with Google maps 7,0
hence you could change the uri to :
Second option:
String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";
where mTitle
is the name of the location.
Third option:
geo:0,0?q=my+street+address
Fourth option:
String map = "http://maps.google.co.in/maps?q=" + yourAddress;
Hope that works and helps :D..
Related Topics
Display Fb Profile Pic in Circular Image View in Application
Android Getorientation Azimuth Gets Polluted When Phone Is Tilted
Android - Way to Appear Bordered Text on the Textview
How to Display Fetched JSON Data into Listview Using Baseadapter
Why Can't One Add/Remove Items from an Arrayadapter
How to Prevent Multiple Toast Overlaps
Get Last Inserted Value from SQLite Database Android
Android Mediaplayer/Videoview Error (1, -2147483648)
How to Parse the CSV File in Android Application
Firebase Sort by Points Depending on Date
How to Use Broadcast Receiver in Different Applications in Android
Garbage Collection Causes:Mediaplayer Finalized Without Being Released
Pass Arraylist Data into Soap Web Service in Android
Android - Build Separate APKs for Different Processor Architectures