activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Liner"
android:background="@drawable/background_login"
>
<com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"
style="@style/Liner"
android:clickable="true" />
</LinearLayout>
MapActivity.java:
package com.example.foreveross.office;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.example.wenandroid.R;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MapActivity extends Activity {
BMapManager mBMapMan=null;
MapView mMapView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBMapMan=new BMapManager(getApplication());
mBMapMan.init("BB3299207a600b8a035b76982adde60f", null);
setContentView(R.layout.activity_main);
mMapView=(MapView)findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true);
//设置启用内置的缩放控件
MapController mMapController=mMapView.getController();
//得到mMapView的控制权,可以用它控制盒驱动平移和缩放
GeoPoint point=new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//用给定的经纬度构造一个GeoPoint,单位是微度(度*1E6)
mMapController.setCenter(point);//设置地图中心点
mMapController.setZoom(12);//设置地图zoom级别
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mMapView.destroy();
if(mBMapMan!=null){
mBMapMan.destroy();
mBMapMan=null;
}
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mMapView.onPause();
if(mBMapMan!=null){
mBMapMan.stop();
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
mMapView.onResume();
if(mBMapMan!=null){
mBMapMan.start();
}
super.onResume();
}
}
api KEY的安全码必须是每个项目对应一个安全码。申请安全码时包名为项目配置文件中的packagename.
定位Activity代码:(未实现)
package com.example.foreveross.office;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.example.wenandroid.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MapActivity extends Activity {
BMapManager mBMapMan=null;
MapView mMapView=null;
MapController mMapController;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBMapMan=new BMapManager(getApplication());
mBMapMan.init("BB3299207a600b8a035b76982adde60f", null);
setContentView(R.layout.activity_main);
mMapView=(MapView)findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true);
//设置启用内置的缩放控件
mMapController=mMapView.getController();
//得到mMapView的控制权,可以用它控制盒驱动平移和缩放
GeoPoint point=new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//用给定的经纬度构造一个GeoPoint,单位是微度(度*1E6)
mMapController.setCenter(point);//设置地图中心点
mMapController.setZoom(12);//设置地图zoom级别
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "No address found";
if (location != null) {
// Update the map location.
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(),
geoLng.intValue());
mMapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString + "\n" + addressString);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mMapView.destroy();
if(mBMapMan!=null){
mBMapMan.destroy();
mBMapMan=null;
}
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mMapView.onPause();
if(mBMapMan!=null){
mBMapMan.stop();
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
mMapView.onResume();
if(mBMapMan!=null){
mBMapMan.start();
}
super.onResume();
}
}