在谷歌地图android上显示位置

Posted

技术标签:

【中文标题】在谷歌地图android上显示位置【英文标题】:show location on google map android 【发布时间】:2015-05-11 12:46:12 【问题描述】:

我有一个EditText 和一个Button 我想在地图中查找位置。我正在使用AsyncTask 执行这里是我对AsyncTask 类的调用。当我使用Toast msg 时,它会显示我的位置,但在地图中它被强制关闭

new GeocoderTask().execute(location);
Toast.makeText(getApplicationContext(), location, Toast.LENGTH_LONG).show();

这是我的 GeocoderTask 类,用于在地图中显示位置

private class GeocoderTask extends AsyncTask<String, Void, List<Address>>

            @Override
            protected List<Address> doInBackground(String... locationName) 
                // Creating an instance of Geocoder class
                Geocoder geocoder = new Geocoder(getBaseContext());
                List<Address> addresses = null;

                try 
                    // Getting a maximum of 3 Address that matches the input text
                    addresses = geocoder.getFromLocationName(locationName[0], 3);
                 catch (IOException e) 
                    e.printStackTrace();
                           
                return addresses;
            


            @Override
            protected void onPostExecute(List<Address> addresses)          

                if(addresses==null || addresses.size()==0)
                    Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
                

                // Clears all the existing markers on the map
                googleMap.clear();

                // Adding Markers on Google Map for each matching address
                for(int i=0;i<addresses.size();i++)                

                    Address address = (Address) addresses.get(i);

                    // Creating an instance of GeoPoint, to display in Google Map
                    latLng = new LatLng(address.getLatitude(), address.getLongitude());

                    String addressText = String.format("%s, %s",
                            address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                            address.getCountryName());

                    markerOptions = new MarkerOptions();
                    markerOptions.position(latLng);
                    markerOptions.title(addressText);

                    googleMap.addMarker(markerOptions);

                    // Locate the first location
                    if(i==0)                        
                        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));     
                           
                   
        

但它不显示位置。

【问题讨论】:

请任何人帮助我..... 你有什么问题??? 我想在地图上搜索我的位置 你的代码在哪一行失败了?? 我在尝试搜索时没有任何错误显示强制关闭 【参考方案1】:

假设你在new Geocoder(getBaseContext()); 行有错误,所以建议你使用构造函数将Context 传递给AsyncTask

private class GeocoderTask extends AsyncTask<String, Void, List<Address>>

        private Context mContext;
        public GeoCoderTask(Context context) 
            super();
            mContext = context;
        


        @Override
        protected List<Address> doInBackground(String... locationName) 
            // Creating an instance of Geocoder class
            Geocoder geocoder = new Geocoder(mContext);

            ... ...

        

希望对您有所帮助!

【讨论】:

以上是关于在谷歌地图android上显示位置的主要内容,如果未能解决你的问题,请参考以下文章

如何在谷歌地图android中显示当前位置的平滑移动

Android - 我如何在谷歌地图中实现位置准确性

无法在谷歌地图android上动态设置多个标记

如何在谷歌地图android上设置多个标记?

在谷歌地图android中更改标记的原点

在谷歌地图上隐藏其他位置