首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓地图在远离蓝点的地方添加currentLocation标记

安卓地图在远离蓝点的地方添加currentLocation标记
EN

Stack Overflow用户
提问于 2017-03-07 15:34:35
回答 1查看 320关注 0票数 0

我正在开发谷歌地图应用程序接口v2。我有一个向我当前位置添加标记的按钮。

地图可以获得正确的位置,但有时(不是每次)当我想要添加标记到该位置时,标记会远离蓝点。

我想在蓝点上添加我的记号笔。

这是我的findMe按钮代码:

代码语言:javascript
复制
ImageButton findMyLocation_btn = (ImageButton) findViewById(R.id.findme);
    if (findMyLocation_btn != null) {
        findMyLocation_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    buildAlertMessageNoGps();
                } else {
                    currentLocation = mMap.getMyLocation();

                    if ( marker1 == null && currentLocation != null) {
                        new onMyLocationClick().execute();
                    }
                }
            }
        });

和onMyLocationClick()

代码语言:javascript
复制
private class onMyLocationClick extends AsyncTask{

    @Override
    protected Object doInBackground(Object[] params) {


            Geocoder gc = new Geocoder(MainActivity.this);
            List<Address> list = null;
            try {
                list = gc.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            if (list != null) {
                add = list.get(0);
            }
            try {
                origin = new LatLng(add.getLatitude(),add.getLongitude());
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        return null;
    }

    @Override
    protected void onPostExecute (Object o){

            createMarker(add.getLatitude(), add.getLongitude(), add.getLocality(), add.getSubLocality());
        }
}
EN

回答 1

Stack Overflow用户

发布于 2017-03-07 15:44:05

基于Google Maps文档:

此方法已弃用。请改用com.google.android.gms.location.FusedLocationProviderApi。FusedLocationProviderApi提供了改进的位置查找和电源使用,并由“我的位置”蓝点使用。

获取当前位置的代码如下所示:

代码语言:javascript
复制
LocationManager locationManager = (LocationManager)
        getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager
        .getBestProvider(criteria, false));
double latitude = location.getLatitude();
double longitude = location.getLongitude();

对于更高级的解决方案,你可以使用谷歌提到的FusedLocationProvider:https://developer.android.com/training/location/retrieve-current.html#GetLocation

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42642684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档