0

i am working on google maps. the app sucesssfully trace user location but when i search for place it doesn't return a result. below is my code along with manifest and gradle.build. Also logcat is also attached which confirms the geolocate function execute as geolocating is displayed. but no place is returned. MapsActivity.java

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            mSearchText = (EditText) findViewById(R.id.input_search);
            mGps = (ImageView) findViewById(R.id.gps_ic);


            mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override

                public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                    if(actionId == EditorInfo.IME_ACTION_SEARCH
                            || actionId == EditorInfo.IME_ACTION_DONE
                            || keyEvent.getAction() == KeyEvent.ACTION_DOWN
                            || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                        //execute our method for searching
                        geoLocate();
                        return true;
                    }

                    return false;
                }
            });
        }
     private void geoLocate(){

            Log.d(TAG, "geoLocate: geolocating");

            String searchString = mSearchText.getText().toString();

            Geocoder geocoder = new Geocoder(MapsActivity.this);
            List<Address> list = new ArrayList<>();
            try{
                Toast.makeText(MapsActivity.this, searchString, Toast.LENGTH_SHORT).show();
                list = geocoder.getFromLocationName(searchString, 1);
            }catch (IOException e){
                Log.e(TAG, "geoLocate: IOException: " + e.getMessage() );
            }

            if(list.size() > 0){
                Address address = list.get(0);

                Log.d(TAG, "geoLocate: found a location: " + address.toString());
                //Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();

                moveCamera(new LatLng(address.getLatitude(), address.getLongitude()), DEFAULT_ZOOM,
                        address.getAddressLine(0));
            }
        }

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smartcourier.scs">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <permission
        android:name="com.project.googlemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launchernew"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity android:name=".Login"></activity>
        <activity android:name=".LoginCustomer"></activity>
        <activity android:name=".MainMenu">

        </activity>
        <activity android:name=".PackageDetail"></activity>
        <activity android:name=".LoginDriver" />
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->


        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.smartcourier.scs"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.android.gms:play-services-maps:11.8.0'
    compile 'com.google.firebase:firebase-core:11.8.0'
    compile 'com.google.firebase:firebase-auth:11.8.0'
    compile 'com.firebaseui:firebase-ui:3.1.3'
    compile 'com.firebaseui:firebase-ui-auth:3.1.3'
    compile 'com.google.android.gms:play-services-location:11.8.0'
    compile 'com.google.android.gms:play-services-places:11.8.0'
    compile 'com.google.maps.android:android-maps-utils:0.5'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
apply plugin: 'com.google.gms.google-services'

logcat

  • Hello @asifa you need to search place using Latitude Longitude – Adil Mar 07 '18 at 06:13
  • @Adil you are wrong - OP is getting known address based on entered Address/Query - https://developer.android.com/reference/android/location/Geocoder.html – Dawood Awan Mar 07 '18 at 06:15
  • have you tried increasing the maxResults parameter? - also doesn't this block the UI thread, because it is a network operation? - https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception – Dawood Awan Mar 07 '18 at 06:24
  • check links 1) https://developers.google.com/places/android-api/autocomplete 2) http://www.viralandroid.com/2016/04/google-maps-android-api-adding-search-bar-part-3.html – pavan kvch Mar 07 '18 at 07:23
  • @Dawood Awan yes i have also tried increasing maxResults but still not working – asifa mahmood Mar 07 '18 at 07:46

2 Answers2

0

you need to search place using Latitude Longitude

for search text to Latitude Longitude you need to implement Google Place Autocomplete

OR search from text

private void geoLocate() {


    final String searchString = mSearchText.getText().toString();
    final Geocoder geocoder = new Geocoder(SetLocationMapActivity.this);
    new Thread(new Runnable() {

        @Override
        public void run() {

            try {
                List<Address> addressList = geocoder.getFromLocationName(searchString, 1);
                if (addressList != null && addressList.size() > 0) {
                    final String locality = addressList.get(0).getAddressLine(0);
                    final String country = addressList.get(0).getCountryName();

                    final Address address = addressList.get(0);


                    if (!locality.isEmpty() && !country.isEmpty()) {
                        try {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    Log.e("tag", "address=" + locality + "  " + country);

                                    moveCamera(new LatLng(address.getLatitude(), address.getLongitude()), DEFAULT_ZOOM,
                                            address.getAddressLine(0));
                                }
                            });
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

OR search from Latitude Longitude

change this line

 List<Address> addressList = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
Adil
  • 812
  • 1
  • 9
  • 29
  • did you try with using latitude and longitude? first check it with static argument of latitude and longitude because using text search you must have a full address – Adil Mar 07 '18 at 07:40
-1
put your PlacesAutoCompleteTextView in xml

<com.yugasa.placesautocomplete.PlacesAutocompleteTextView
            android:id="@+id/autocomplete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="15dp"
            android:hint="Search city"
            app:pacv_languageCode="en"
            android:visibility="gone"

            app:pacv_resultType="no_type"
            app:pacv_clearEnabled="true"
            app:pacv_googleMapsApiKey="@string/google_map_api"
            app:pacv_adapterClass="com.yugasa.piknik.adapters.TestPlacesAutocompleteAdapter"/>

get id of autocomplete texview in your class 



    auto_location.setOnPlaceSelectedListener(new OnPlaceSelectedListener() {
                @Override
                public void onPlaceSelected(@NonNull final com.yugasa.placesautocomplete.model.Place place) {
                    auto_location.getDetailsFor(place, new DetailsCallback() {
                        @Override
                        public void onSuccess(PlaceDetails details) {
                           //here you will get all details of places and can show it on map or in list
                        }

                        @Override
                        public void onFailure(Throwable failure) {

                        }
                    });
                }
            });
Mohit Hooda
  • 273
  • 3
  • 9