没有在地图中搜索到的地方

时间:2018-03-07 05:51:20

标签: android google-maps

我正在研究谷歌地图。应用程序成功跟踪用户位置,但当我搜索地点时,它不会返回结果。下面是我的代码以及manifest和gradle.build。 此外,还附加了logcat,它确认geolocate函数在显示地理定位时执行。但没有地方归还。 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

2 个答案:

答案 0 :(得分:0)

您需要使用纬度经度

搜索地点

搜索文字为纬度经度,您需要实施Google Place Autocomplete

从文字搜索

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();
}

从纬度经度搜索

更改此行

 List<Address> addressList = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

答案 1 :(得分:-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) {

                        }
                    });
                }
            });