Android Google Maps GeoPoint问题

时间:2015-08-06 08:51:07

标签: android google-maps

我知道有几次被问过,但在所有答案中,我找不到解决方案,所以我来问问自己。

在名为“DeAs”的活动中,我有一个地址editText,我想在用户正在编辑的editText下面显示一个潜在地址列表。

首先在Android Manifest中,我写了这个:

<activity android:name=".DeAs">
        <uses-library android:required="true" android:name="com.google.android.maps" />
    </activity>

我已将build.gradle转换为加载Google API:

apply plugin: 'com.android.application'

android {
compileSdkVersion 'Google Inc.:Google APIs:22'
buildToolsVersion '22.0.1'

defaultConfig {
    applicationId "com.example.fabien.rh"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.google.android.gms:play-services:7.5.0'
}

我在editText上声明了一个监听器:

adressete = (EditText) rootView.findViewById(R.id.daadressete);

        adressete.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                new SearchClicked(adressete.getText().toString(),getActivity()).execute();

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

            @Override
            public void afterTextChanged(Editable arg0) {}
        });

首先我写了一个EditorActionListener,但它没有用,我已经读过几个Android版本的editorlisteneer从未调用过。 在这个Listener中,我调用了SearchClicked类,如下所示:

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import android.os.AsyncTask;
import android.util.Log;

import com.google.android.maps.GeoPoint;

import java.util.List;
import java.util.Locale;

public class SearchClicked extends AsyncTask<Void, Void, Boolean> {
private String toSearch;
private Address address;
private Context c;

public SearchClicked(String toSearch, Context c) {
    this.toSearch = toSearch;
    this.c= c;
}

@Override
protected Boolean doInBackground(Void... voids) {

    try {
        Geocoder geocoder = new Geocoder(c, Locale.UK);
        List<Address> results = geocoder.getFromLocationName(toSearch, 1);

        if (results.size() == 0) {
            return false;
        }

        address = results.get(0);

        // Now do something with this GeoPoint:
        GeoPoint p = new GeoPoint((int) (address.getLatitude() * 1E6), (int) (address.getLongitude() * 1E6));

    } catch (Exception e) {
        Log.e("", "Something went wrong: ", e);
        return false;
    }
    return true;
}
}

当我运行它时,它很好地加载,但是当我尝试在edittext上写一些东西时,我有一个错误:

Could not find class 'com.google.android.maps.GeoPoint'
ATAL EXCEPTION: AsyncTask #1
Process: com.example.fabien.rh, PID: 26795
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.NoClassDefFoundError: com.google.android.maps.GeoPoint
        at com.example.fabien.renthelper.SearchClicked.doInBackground(SearchClicked.java:40)
        at com.example.fabien.renthelper.SearchClicked.doInBackground(SearchClicked.java:16)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

关于这个主题,我有可能找到一个解决方案: Libraries do not get added to APK anymore after upgrade to ADT 22 但我没有在Android Studio中找到“订购和导出”,而且我没有“Android私有库”:当我在“项目结构”中查找库时,我所拥有的就是:

"support-annotations (com.android.support-annotations:22.2.1)
support-v4 (com.android.support:support-v4:22.2.1)
support-v13 (com.android.support-v13:22.2.1)
appcompat-v7 (com.android.support:appcompat-v7:22.2.1)
design (com.android.design:22.2.1)
gridlayout-v7 (com.android.support.gridlayout-v7:22.2.1)
mediarouter-v7 (com.android.support:mediarouter-v7:22.2.1)
play-services (com.google.android.gms:play-services:7.5.0)
play-service-wearable (com.google.android.gms:play-services_wearable:7.5.0)
cardview-v7 (com.android.support-cardview-v7:22.2.1)
palette-v7 (com.android.support-palette-v7:22.2.1)
[...]"

如果您能提供帮助,请提前感谢您! ;)

1 个答案:

答案 0 :(得分:1)

我不知道代码是如何编译的,并且从发布的元素来看,你不是100%清楚你是如何在android studio上配置构建的,但无论如何,谷歌地图API v2中没有GeoPoint。

您应该使用LatLng对象: https://developers.google.com/android/reference/com/google/android/gms/maps/model/LatLng