Android百度地图开发之显示当前位置地图 - zhengyikuangge的博客 - 博客频道 - CSDN.NET

来源: Android百度地图开发之显示当前位置地图 – zhengyikuangge的博客 – 博客频道 – CSDN.NET

步骤:

1、获取当前的经纬度
2、显示地图
3、改变地图中心的经纬度


1、获取当前的经纬度
请参考网址:
http://blog.csdn.net/zhengyikuangge/article/details/51921549


2、显示地图
请参考网址:
http://blog.csdn.net/zhengyikuangge/article/details/51945053


3、改变地图中心的经纬度:
参考代码(只提供Java代码,其他代码与第2个网址中的相同):

package com.example.baidumaptest;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends Activity {
private LocationManager locationManager;
private String provider;
MapView mapView;
BaiduMap baiduMap;
boolean ifFrist = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map_view);
// 获取baiduMap对象
baiduMap = mapView.getMap();
// 设置可改变地图位置
baiduMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List list = locationManager.getProviders(true);
if (list.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (list.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;

} else {
Toast.makeText(this, "当前不能提供位置信息", Toast.LENGTH_LONG).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
navigateTo(location);
}
locationManager.requestLocationUpdates(provider, 5000, 1,
locationListener);

}

private void navigateTo(Location location) {
// 按照经纬度确定地图位置
if (ifFrist) {
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
// 移动到某经纬度
baiduMap.animateMapStatus(update);
update = MapStatusUpdateFactory.zoomBy(5f);
// 放大
baiduMap.animateMapStatus(update);

ifFrist = false;
}
// 显示个人位置图标
MyLocationData.Builder builder = new MyLocationData.Builder();
builder.latitude(location.getLatitude());
builder.longitude(location.getLongitude());
MyLocationData data = builder.build();
baiduMap.setMyLocationData(data);
}

LocationListener locationListener = new LocationListener() {

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
// 位置改变则重新定位并显示地图
navigateTo(arg0);
}
};

@Override
protected void onDestroy() {
// 释放资源
super.onDestroy();
if (locationManager != null) {
locationManager.removeUpdates(locationListener);
}

mapView.onDestroy();

baiduMap.setMyLocationEnabled(false);

}

}

参考代码下载地址:
http://download.csdn.net/detail/zhengyikuangge/9579595

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏