如何在百度地图android中显示当前位置

时间:2015-08-27 12:35:30

标签: android google-maps location baidu

我想在Android应用中使用百度地图。到目前为止,我已经设法整合百度地图,但它显示了所有纬度和经度的静态地图。到目前为止我所做的是:

public class BaiduMapActivity extends AppCompatActivity{

    @SuppressWarnings("unused")
    private static final String LTAG = BaiduMapActivity.class.getSimpleName();
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    // ********bejing**********
    double lat = 39.9167;
    double longi = 116.3833;

    double shanghaiLat = 31.2000;
    double shangaiLong = 121.5000;
    Context mContext = BaiduMapActivity.this;
    RoutePlanSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
    RouteLine route = null;
    OverlayManager routeOverlay = null;
    boolean useDefaultIcon = false;
    private boolean canGetLocation;
    private double singaporeLat = 1.3000;
    private double singaporeLong = 103.8000;
    private double hongkongLat = 22.2783;
    private double hongkongLong = 114.1747;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_baidu_maps_layout);
        init();
        try {
            mBaiduMap = mMapView.getMap();
            mBaiduMap.setMyLocationEnabled(true);
            MyLocationData locData = new MyLocationData.Builder()

                    .direction(100).latitude(hongkongLat).longitude(hongkongLong).build();  
          //loads same map even if lat long are changed
            mBaiduMap.setMyLocationData(locData);

        }catch (Exception e){
            e.printStackTrace();

        }
    }


    private void init() {
        mMapView = (MapView) findViewById(R.id.baidu_mapview_id);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

}

如何加载不同纬度的地图?

1 个答案:

答案 0 :(得分:0)

您应该更改此行:

MyLocationData locData = new MyLocationData.Builder().direction(100).latitude(hongkongLat).longitude(hongkongLong).build();

您可以将hongkongLat更改为lat,将hongkongLong更改为您的长途。

但有一点需要注意的是,百度地图使用了不同的纬度/经度系统。他们提供API to translate "Google's lat/lng" to "Baidu's lat/lng"。但是,使用Google地图总是更容易。