没有在地图中搜索到的地方 [英] not getting searched place in maps
问题描述
我正在Google地图上工作。该应用程序成功跟踪用户的位置,但是当我搜索的地方它不会返回结果。下面是我的代码以及manifest和gradle.build。
还附加了logcat,确认地理定位功能在地理定位显示时执行。但没有地方返回。
MapsActivity.java
@Override
保护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){
//执行我们的搜索
的方法geoLocate();
返回true;
}
返回false;
}
});
private void geoLocate(){
Log.d(TAG,geoLocate:geolocating);
String searchString = mSearchText.getText()。toString();
Geocoder geocoder =新的地理编码器(MapsActivity.this);
列表<地址> list = new ArrayList<>();
尝试{
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();
$ b $ moveCamera(new LatLng(address.getLatitude(),address.getLongitude()),DEFAULT_ZOOM,
address.getAddressLine(0));
$ / code $ / pre
$ b $ Manifest.xml
<?xml version =1.0encoding =utf-8?>
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.example.smartcourier.scs>
<! - -
使用
Google地图Android API v2时不需要ACCESS_COARSE / FINE_LOCATION权限,但您必须指定粗略或精细的
位置'MyLocation'功能的权限。
- >
<使用权限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/>
<! -
Google Maps API API密钥定义为字符串资源。
(请参阅文件res / values / google_maps_api.xml)。
请注意,API密钥已链接到用于签署APK的加密密钥。
您需要为每个加密密钥使用不同的API密钥,包括用于
签名的发布密钥。
您可以在src / debug /和src / release /中定义调试和发布目标的键。
- >
< activity
android:name =。MapsActivity
android:label =@ string / title_activity_maps>
< intent-filter>
解决方案您需要使用纬度经度
为搜索文本搜索纬度经度,您需要实现 Google Place Autocomplete
或从文本搜索
private void geoLocate(){
final String searchString = mSearchText.getText()。toString();
final Geocoder geocoder = new Geocoder(SetLocationMapActivity.this);
新线程(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();
$ b $ final地址= 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();
}
或从纬度经度搜索
更改此行
列表<地址> addressList = geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
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'
解决方案 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);
这篇关于没有在地图中搜索到的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!