Red de conocimiento informático - Material del sitio web - Cómo llamar a Baidu Map API usando Android

Cómo llamar a Baidu Map API usando Android

1. Echemos un vistazo al código Java. El código de referencia de MainActivity.class es el siguiente:

1 clase pública MainActivity extiende MapActivity {

2 // La ubicación inicial es. establecido en Xi'an

3 usuarios dobles privadosLongitude = 34.341568 * 1E6;// Latitud

4 usuarios dobles privadosLatitud = 108.94017499999995 * 1E6;// Longitud

5 // Agregar controles relacionados con Baidu

6 Private MapView mapView

7 Private BMapManager bMapManager // Motor para cargar mapas

8 // Valor clave de Mapa de Baidu

9 private String keyString = " 01331AFA954E7E300428A5F0C9C829E0E16F87A3";

10 // Agrega algunos controles en el mapa de Baidu, como acercar y alejar

11 private MapController mapController;

12 MKLocationManager privado mLocationManager;

13

14 @Override

15 public void onCreate(Bundle saveInstanceState) {

16 super.onCreate (savedInstanceState);

17 setContentView(R.layout.activity_main);

18 // Crear una instancia del control

19 mapView = (MapView) this.findViewById (R.id.bmapView);

20 bMapManager = new BMapManager(MainActivity.this);

21 // La clave debe ser cargado

22 bMapManager .init(keyString, new MKGeneralListener() {

23 // El valor de la clave es incorrecto

24 @Override

25 public void onGetPermissionState(int arg0) {

26 if (arg0 == 300) {

27 Toast.makeText(MainActivity.this, R.string.key_error,Toast .LENGTH_LONG).show();

p>

28 }

29 }

30 // Valor de clave incorrecto.

29 }

30 // Error de red

31 @Override

32 public void onGetNetworkState(int arg0) {

33 Toast.makeText(MainActivity.this, R.string.net_ error,Toast.LENGTH_LONG).show()

34 }

35 }.

35 });

36 this.initMapActivity(bMapManager);

37 mapView.requestLocationUpdates(locationListener

46 mLocationManager.setNotifyInternal); (5, 2);

47 // Agregar la capa de ubicación

48 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,mapView

49 myLocationOverlay.enableMyLocation); (); // habilitar ubicación

50 myLocationOverlay.

51 mapView.getOverlays().add( myLocationOverlay

52 mapView.setTraffic(true) ;// Mapa de tráfico

53 // mapView.setSatellite(true);// Mapa satelital

54 mapController.setZoom(15);// Establecer nivel de zoom

55 mapView.invalidate(); // Actualizar el mapa

56 }

57 // Establecer el nivel de zoom del mapa.

57 // Obtener la latitud y longitud

58 private LocationListener locationListener = new LocationListener() {

59 @Override

60 public void onLocationChanged (Ubicación) {

61 if (ubicación! = nulo) {

62 userLatitude = ubicación.getLatitude() * 1E6; userLongitude = ubicación .getLongitude() * 1E6;

64 GeoPoint mypoint = new GeoPoint((int)(userLatitude), (int)(userLongitude));

65 mapView.getController (). animateTo(mipunto);

66 }

67 }.

67 }

68 };

69 // Destruir

70 @Override

71 protected void onDestroy() {

72 super.onDestroy(); p> 73 if (bMapManager ! = nulo) {

74 bMapManager.destroy();

75 bMapManager = nulo

76 }

77 }.

77 }

78 // Detener

79 @Override

80 protected void onPause() {

81 super.onPause();

82 if (bMapManager ! = null) {

83 mLocationManager.removeUpdates(locationListener); p> 84 bMapManager.stop();

85 }

86 }

87 // Reiniciar

88 @Override

89 protected void onResume() {

90 super.onResume();

91 if (bMapManager ! = null) {

92 bMapManager.start();

93 }

94 }.

94 }

95 //

96 @Override

97 booleano protegido isRouteDisplayed() { <

98 return false

99 }

100

101

102 // Menú

103 // Se llama cuando se hace clic en el botón de menú

104 @Override

105 booleano público en Crear

OptionsMenu(Menú menú) {

106 menú.

106 menu.add(0, 1, 1, R.string.exit).setIcon(

107 android.R.drawable.ic_menu_close_ clear_cancel);

108 return super.onCreateOptionsMenu(menu);

109 }

110 @Override

111 public boolean onContextItemSelected(MenuItem item) { // Seleccionar elemento del menú

112 if (item.getItemId() == 1) {

113 MainActivity.this.finish ();

114 }

115 return super.onOptionsItemSelected(item);

116 }

117 // Botón de retorno

118 @Override

119 public boolean onKeyDown(int keyCode, evento KeyEvent) {

120 if (keyCode == KeyEvent.KEYCODE_BACK) {

121 finalizar();

122 }

123 // Botón regresar.

123 return super.onKeyDown(keyCode, evento

124 }

125 }

126

2. Archivo de diseño, utilizado principalmente para agregar controles para mostrar mapas. El código de referencia de Activity_mainmain.xml es el siguiente:

1

2 xmlns:tools="/tools"

3 android:layout_width="match_parent"

4 android:layout_ height="match_parent" >

5

6