百度地圖加載顯示

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();
	}
	

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章