Android 百度、高德地图显示及点击获取经度纬度;只有经纬度没有其他地理信息的表示账号值配错了

一、Android studio环境搭建地址:

1、http://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-project/androidstudio

 

二、复制粘贴:

1、布局:

<com.baidu.mapapi.map.MapView
    android:id="@+id/bmapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"/> 

 

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    SDKReceiver mReceiver;
    MapView mapView;
    private BaiduMap baiduMap = null;
    LatLng point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFilter();
        initMap();
    }

    private void initView(){
        mapView=findViewById(R.id.bmapView);
        if (baiduMap == null) {
            baiduMap = mapView.getMap();
        }
    }

    private void initMap() {
        BaiduMap.OnMapClickListener listener = new BaiduMap.OnMapClickListener() {
            /**
             * 地图单击事件回调函数
             *
             * @param point 点击的地理坐标
             */
            @Override
            public void onMapClick(LatLng point) {
                Log.e(TAG, "onMapClick latitude: "+point.latitude );//经度
                Log.e(TAG, "onMapClick longitude: "+point.longitude );//纬度
                setMarkPoint(point.latitude,point.longitude);
            }

            /**
             * 地图内 Poi 单击事件回调函数
             *
             * @param mapPoi 点击的 poi 信息
             */
            @Override
            public void onMapPoiClick(MapPoi mapPoi) {
                Log.e(TAG, "onMapPoiClick getName: "+mapPoi.getName() );
                Log.e(TAG, "onMapPoiClick getUid: "+mapPoi.getUid() );
                Log.e(TAG, "onMapPoiClick getPosition: "+mapPoi.getPosition() );
            }
        };//点击获取经纬度;
        baiduMap.setOnMapClickListener(listener);
        baiduMap.setMyLocationEnabled(true);
    }
    private void setMarkPoint(double jingdu,double weidu) {
        //定义Maker坐标点
        baiduMap.clear();
        point = new LatLng(jingdu, weidu);
//构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.baiduselect_icon);
//构建MarkerOption,用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
//在地图上添加Marker,并显示
        baiduMap.addOverlay(option);
    }

    private void initFilter() {
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
        iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK);
        mReceiver = new SDKReceiver();
        registerReceiver(mReceiver, iFilter);
    }
}


/**-------------------------------高德地图---------------------*/
    private void initLocal() {

        /**
         *  初始化定位并获取回调
         */
        //初始化定位
        mLocationClient = new AMapLocationClient(this);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息(默认返回地址信息)
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI,默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        //启动定位
        mLocationClient.startLocation();
    }

    private AMapLocationClient mLocationClient = null;//高德定位

private double currentLat;//获取纬度
    private double currentLon;//获取经度
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            // 从这里开始就会持续回调
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //定位成功回调信息,设置相关消息
                    amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                    currentLat = amapLocation.getLatitude();//获取纬度
                    currentLon = amapLocation.getLongitude();//获取经度
//                    currentLatLng = new LatLng(currentLat, currentLon);
                    amapLocation.getAccuracy();//获取精度信息
//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    Date date = new Date(amapLocation.getTime());
//                    df.format(date);//定位时间
//                    Log.e(TAG, "onLocationChanged 时间: "+df.format(date) );
                    Log.e(TAG, "currentLat : " + currentLat + " currentLon : " + currentLon);
                    City = amapLocation.getCity() ;
                    Province=amapLocation.getProvince();

                    if (!isSearch && City != null) {
                        getCityWeather(City);
                        isSearch = true;
                    }
                } else {
                    //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    };

private void getCityWeather(String city) {
        Log.e(TAG, "getCityWeather: ");
        //检索参数为城市和天气类型,实况天气为WEATHER_TYPE_LIVE、天气预报为WEATHER_TYPE_FORECAST
        WeatherSearchQuery mquery = new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE);
        WeatherSearch mweathersearch = new WeatherSearch(this);
        mweathersearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
            @Override
            public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
                Log.e(TAG, "onWeatherLiveSearched: " + localWeatherLiveResult.toString());
                Weather=localWeatherLiveResult.getLiveResult().getWeather();
                WeatherTemp=localWeatherLiveResult.getLiveResult().getTemperature();
                handler.sendEmptyMessage(initWeatherType);
            }

            @Override
            public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {
                Log.e(TAG, "onWeatherForecastSearched: " + localWeatherForecastResult.toString());
                }
        });
        mweathersearch.setQuery(mquery);
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                mweathersearch.searchWeatherAsyn(); //异步搜索
                handler.sendEmptyMessage(initCityType);
            }
        });
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值