java - getFromLocationName().size() 始终返回 0 - Android

标签 java android google-maps geocoding location-based

在我的应用程序中,我有一个 Activity ,您可以在其中根据您在 edittext 中输入的地址在 map 上固定位置,或者您可以只移动 onMapReady 函数中显示的固定位置。

<小时/>

我的 Activity 布局包含显示 map 的 fragment 、一个用于返回有关上一个 Activity 中图钉位置信息的按钮以及一个编辑文本,在输入地址并按下按钮后, map 上的上一个图钉为被删除,并且会出现一个新的位置,其中包含您键入的位置(如果存在)。

<小时/>

-- 这是我的 onMapReady 函数,它在搜索按钮中有一个监听器,并初始化图钉以及在拖动图钉后获取图钉位置的函数:

 @Override
    public void onMapReady(final GoogleMap map) {
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
        map.animateCamera(zoom);

        map.addMarker(new MarkerOptions()
                .title("Shop")
                .snippet("Is this the right location?")
                .position(position))
                .setDraggable(true);

        // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
        map.setOnInfoWindowClickListener(this);
        map.setOnMarkerDragListener(this);

        ImageButton search = (ImageButton) findViewById(R.id.search);

        final EditText searchaddress = (EditText) findViewById(R.id.locationsearch);

        final int[] numAttempts = {0};

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //FIND LOCATION BY ADDRESS
                Geocoder geoCoder = new Geocoder(MapsActivity.this.getApplicationContext(), Locale.getDefault());
//                try {
                if (searchaddress.getText().toString() != null && !searchaddress.getText().toString().isEmpty()) {
                    try {
                        List<Address> geoResults = geoCoder.getFromLocationName(searchaddress.getText().toString(), 1);

                        if (geoResults != null) {

                          //  do {
                          //      geoResults = geoCoder.getFromLocationName(searchaddress.getText().toString(), 1);

                            //    numAttempts[0] = numAttempts[0] + 1;
                         //   }
                         //   while (geoResults.size() == 0 || numAttempts[0] == 5);

                            if (geoResults.size() > 0) {
                                Address addr = geoResults.get(0);

                                if (!addr.toString().isEmpty()) {
                                    double latitude = addr.getLatitude();
                                    double longitude = addr.getLongitude();

                                    position = new LatLng(latitude, longitude);

                                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
                                    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
                                    map.animateCamera(zoom);

                                    //Marker marker = null;
                                    map.clear();
                                    //marker.setPosition(position);
                                    map.addMarker(new MarkerOptions()
                                            .title("Shop")
                                            .snippet("Is this the right location?")
                                            .position(position))
                                            .setDraggable(true);

                                    // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
                                    map.setOnInfoWindowClickListener(MapsActivity.this);
                                    map.setOnMarkerDragListener(MapsActivity.this);
                                } else {
                                    Toast.makeText(getApplicationContext(), "Addr is empty!", Toast.LENGTH_LONG).show();
                                }
                            } else {
                                Toast.makeText(getApplicationContext(), "Wrong address. Please try another one! :" + geoResults.size() + "!", Toast.LENGTH_LONG).show();
                            }
                        }
                        else {
                            Toast.makeText(getApplicationContext(), "Null address!", Toast.LENGTH_LONG).show();
                        }
                    }catch(Exception e) {
                        Log.e("GEOCODER", e.getMessage(), e);
                    }
    } else {
                        Toast.makeText(getApplicationContext(), "Please enter an address!", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        @Override
        public void onInfoWindowClick(Marker marker) {
            //Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onMarkerDragStart(Marker marker) {
            LatLng position0 = marker.getPosition();

            Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f",
                    position0.latitude,
                    position0.longitude));
        }

        @Override
        public void onMarkerDrag(Marker marker) {
            LatLng position0 = marker.getPosition();

            Log.d(getClass().getSimpleName(),
                    String.format("Dragging to %f:%f", position0.latitude,
                            position0.longitude));

        }

        @Override
        public void onMarkerDragEnd(Marker marker) {
            position = marker.getPosition();

            Log.d(getClass().getSimpleName(), String.format("Dragged to %f:%f",
                    position.latitude,
                    position.longitude));
        }
<小时/>

直到几天前,我还可以在用户使用上述代码输入编辑文本的位置显示一个图钉。现在,由于未知的原因,我无法再使用完全相同的代码来执行此操作。

总是出现关于“地址错误”的toast。请尝试另一个!即使我在编辑文本中输入的地址存在,但geoResults的大小始终为0,Logcat中也没有出现异常

<小时/> <小时/>

-- 当我从注释中删除 do-while 部分时,logcat 中的异常如下:

02-24 13:13:53.251  28017-28024/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Jit: resizing JitTable from 4096 to 8192
02-24 13:14:57.465  28017-28020/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ threadid=3: reacting to signal 3
02-24 13:14:57.765  28017-28020/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
02-24 13:15:02.701  28017-28017/guide_me_for_all.guide_me_for_all A/libc﹕ Fatal signal 6 (SIGABRT) at 0x000008d2 (code=0), thread 28017 (uide_me_for_all)
02-24 13:15:06.605  28881-28881/guide_me_for_all.guide_me_for_all D/ActivityThread﹕ handleBindApplication:guide_me_for_all.guide_me_for_all
02-24 13:15:06.605  28881-28881/guide_me_for_all.guide_me_for_all D/ActivityThread﹕ setTargetHeapUtilization:0.75
02-24 13:15:06.605  28881-28881/guide_me_for_all.guide_me_for_all D/ActivityThread﹕ setTargetHeapMinFree:524288
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 11956: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 11962: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-24 13:15:07.306  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-24 13:15:07.306  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 9598: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-24 13:15:07.306  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-24 13:15:07.316  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-24 13:15:07.316  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 570: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-24 13:15:07.316  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-24 13:15:07.326  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-24 13:15:07.326  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 592: Landroid/content/res/TypedArray;.getType (I)I
02-24 13:15:07.326  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-24 13:15:07.536  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:07.546  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:07.556  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.147  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.147  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.147  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.157  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.167  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.167  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.577  28881-28881/guide_me_for_all.guide_me_for_all D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
02-24 13:15:08.587  28881-28881/guide_me_for_all.guide_me_for_all D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
02-24 13:15:08.597  28881-28881/guide_me_for_all.guide_me_for_all D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
02-24 13:15:08.627  28881-28881/guide_me_for_all.guide_me_for_all D/OpenGLRenderer﹕ Enabling debug mode 0
02-24 13:15:08.647  28881-28881/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 59 frames!  The application may be doing too much work on its main thread.
02-24 13:15:09.598  28881-28881/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41f5b4e8 time:26642276

在这一行中:

12386-12386/guide_me_for_all.guide_me_for_all A/libc﹕ Fatal signal 6 (SIGABRT) at 0x000008d2 (code=0), thread 12386 (uide_me_for_all) ,

我不知道括号里的这个东西是什么,但我的包名称是guide_me_for_all而不是uide_me_for_all。 (如果与此有关)

<小时/>

如果有人能帮我找到以上所有内容的含义,我将非常感激。

<小时/>

修正上述代码后更新: 将位置从类返回到上一个 Activity 的代码(未解决 - 始终获取 NULL 值):

在类里面:

@Override
    protected void onPostExecute(LatLng result) {
        Log.i("GEOCODE", result.toString());

        Intent i = new Intent(this.mainContxt , MapsActivity.class );
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        i.putExtra( "latlng" , result );

        this.mainContxt.startActivity(i);

        super.onPostExecute(result);
    }

在 MapsActivity 的 onCreate 中,我有以下内容:

 if (getIntent() != null) {
                Intent intent = getIntent();null){
                    position_from_address = intent.getStringExtra("latlng");
}

之后,在我的 Activity 的搜索按钮的 onClickListener 中,我得到如下位置:

if (position_from_address != null){
                        String[] latlong = position_from_address.split(",");
                        double latitude = Double.parseDouble(latlong[0]);
                        double longitude = Double.parseDouble(latlong[1]);

                        position = new LatLng(latitude, longitude);

最佳答案

问题可能是查找不是在后台线程中完成的,请尝试从我的答案中复制粘贴代码:Google Geocoder service is unavaliable (Coordinates to address)

然后运行 ​​new GetAddressPositionTask().execute("someAddress") 并看看会发生什么。

将上下文传递给 AsyncTask:

添加构造函数:

public class GetAddressPositionTask extends
    AsyncTask<String, Integer, LatLng> {

     private Context context;

     public GetAddressPositionTask(Context context) {
         this.context = context;
     }

     // .. rest of the code here

}

用法:

GetAddressPositionTask(context).execute("someAddress")

其中 contextgetApplicationContext()getActivity().getApplicationContext()(如果从 fragment 调用)。

关于空指针:

不是 100% 清楚你在哪里获得空指针,但你应该能够这样做:

// in onPostExecute    
i.putParcelable("latlng", result);

MapsActivity

LatLng position = (getIntent() != null) ? getParcelable("latlng") : null;

另一个值得关注的地方是this.mainContxt。只需执行 YourActivity.thisgetApplicationContext() 即可。如果您位于 Fragment 中,则执行 Context context = getActivity() 并确保它不为 null。

关于java - getFromLocationName().size() 始终返回 0 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664057/

相关文章:

java - 正则表达式匹配所有不以数字开头的单词

android - 在 Galaxy S3 上暂停 GLSurfaceView 后,只有文本可见 - 移动时文本模糊。没有按钮背景

android - 异常 : java. lang.RuntimeException: Canvas :尝试使用回收位图

ios - Ionic Native 尝试访问 GoogleMaps 插件,但未安装

google-maps - 用于地理编码的 map API key 不起作用

java - 如何在 Android 中将自动完成搜索与谷歌地图集成?

java - 将 java.util.Date 转换为 YYYY-MM-DD HH :MM:SS format to compatible with MySQL DATETIME

java - Eclipse - 加载 JNI 共享库失败

html - 使用 Java API 将 .html 文件转换为 .mhtml

android - 如何删除与SourceSets相关的Android Lint警告