android加载地图api
㈠ android 百度地图api定位不准确
不是很了解高德地图是不是把位置获取的函数自己封装起了了哦。
这样的话,自己提前获取可能只是做了些无用功而已哦。
不过你还是可以尝试一下啊。启动的时候尝试获取一下位置就好了。
直接在代码里面
new Thead(){
public void run(){
//写你的获取位置代码
}
}.start();
就可以哦。
㈡ 如何使用Android调用百度地图API
1.先来看看java代码,MainActivity.class参考代码如下:
1 public class MainActivity extends MapActivity {
2 // 初始位置设置为西安
3 private double userLongitude = 34.341568 * 1E6;// 纬度
4 private double userLatitude = 108.94017499999995 * 1E6;// 经度
5 // 添加网络相关控件
6 private MapView mapView;
7 private BMapManager bMapManager;// 加载地图的引擎
8 // 网络地图上的key值
9 private String keyString = "";
10 // 在网络地图上添加一些控件,例如放大、缩小
11 private MapController mapController;
12 private MKLocationManager mLocationManager;
13
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_main);
18 // 实例化控件
19 mapView = (MapView) this.findViewById(R.id.bmapView);
20 bMapManager = new BMapManager(MainActivity.this);
21 // 必须要加载key
22 bMapManager.init(keyString, new MKGeneralListener() {
23 // key值不正确
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();
28 }
29 }
30 //网络出错
31 @Override
32 public void onGetNetworkState(int arg0) {
33 Toast.makeText(MainActivity.this, R.string.net_error,Toast.LENGTH_LONG).show();
34 }
35 });
36 this.initMapActivity(bMapManager);
37 mapView.setBuiltInZoomControls(true);// 表示可以设置缩放功能
38 mapController = mapView.getController();
39 // 初始化Location模块
40 mLocationManager = bMapManager.getLocationManager();
41 // 通过enableProvider和disableProvider方法,选择定位的Provider
42 mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
43 mLocationManager.disableProvider(MKLocationManager.MK_GPS_PROVIDER);
44 //返回手机位置
45 mLocationManager.requestLocationUpdates(locationListener);
46 mLocationManager.setNotifyInternal(5, 2);
47 // 添加定位图层
48 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,mapView);
49 myLocationOverlay.enableMyLocation(); // 启用定位
50 myLocationOverlay.enableCompass(); // 启用指南针
51 mapView.getOverlays().add(myLocationOverlay);
52 mapView.setTraffic(true);// 交通地图
53 // mapView.setSatellite(true);// 卫星地图
54 mapController.setZoom(15);// 设置缩放级别
55 mapView.invalidate();// 刷新地图
56 }
57 //获取经度纬度
58 private LocationListener locationListener = new LocationListener() {
59 @Override
60 public void onLocationChanged(Location location) {
61 if (location != null) {
62 userLatitude = location.getLatitude() * 1E6;
63 userLongitude = location.getLongitude() * 1E6;
64 GeoPoint mypoint = new GeoPoint((int)(userLatitude), (int)(userLongitude));
65 mapView.getController().animateTo(mypoint);
66 }
67 }
68 };
69 // 销毁
70 @Override
71 protected void onDestroy() {
72 super.onDestroy();
73 if (bMapManager != null) {
74 bMapManager.destroy();
75 bMapManager = null;
76 }
77 }
78 // 停止
79 @Override
80 protected void onPause() {
81 super.onPause();
82 if (bMapManager != null) {
83 mLocationManager.removeUpdates(locationListener);
84 bMapManager.stop();
85 }
86 }
87 // 重启
88 @Override
89 protected void onResume() {
90 super.onResume();
91 if (bMapManager != null) {
92 bMapManager.start();
93 }
94 }
95 //
96 @Override
97 protected boolean isRouteDisplayed() {
98 return false;
99 }
100
101
102 // Menu
103 // 当点击Menu按钮时,调用该方法
104 @Override
105 public boolean onCreateOptionsMenu(Menu menu) {
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) { // 选中某个菜单项
112 if (item.getItemId() == 1) {
113 MainActivity.this.finish();
114 }
115 return super.onOptionsItemSelected(item);
116 }
117 // 返回键
118 @Override
119 public boolean onKeyDown(int keyCode, KeyEvent event) {
120 if (keyCode == KeyEvent.KEYCODE_BACK) {
121 finish();
122 }
123 return super.onKeyDown(keyCode, event);
124 }
125 }
126
2.布局文件,主要是添加显示地图的控件,activity_mainmain.xml参考代码如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent" >
5 <!-- 添加显示网络地图的控件 -->
6 <com..mapapi.MapView
7 android:id="@+id/bmapView"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent"
10 android:clickable="true"
11 tools:context=".MainActivity" />
12 </LinearLayout>
3.设置配置文件,AndroidManifest.xml参考代码如下:
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2 package="yu.hong.map"
3 android:versionCode="1"
4 android:versionName="1.0" >
5 <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="15" />
6 <!--添加网络地图开发授权 -->
7 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
8 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
9 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
10 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
11 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
12 <uses-permission android:name="android.permission.INTERNET" />
13 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
14 <!--添加对屏幕的支持 -->
15 <supports-screens
16 android:anyDensity="true"
17 android:largeScreens="true"
18 android:normalScreens="true"
19 android:resizeable="true"
20 android:smallScreens="true" />
21 <application
22 android:icon="@drawable/ic_maplauncher"
23 android:label="@string/app_name"
24 android:theme="@style/AppTheme" >
25 <activity
26 android:name=".MainActivity"
27 android:label="@string/title_activity_main" >
28 <intent-filter>
29 <action android:name="android.intent.action.MAIN" />
30 <category android:name="android.intent.category.LAUNCHER" />
31 </intent-filter>
32 </activity>
33 </application>
34 </manifest>
㈢ Android骞冲彴楂桦痉API缁忛獙锛氶暱鎸夊湴锲捐幏鍙栦綅缃淇℃伅
Android寮鍙戣繃绋嬩腑,链夊緢澶氭坠锷挎搷浣滈兘寰堣╀汉鍙堢埍鍙堟仺銆备竴鏂归溃鍙浠ユ洿渚挎嵎镄勪綋鐜版洿澶氩姛鑳,鎻愬崌搴旂敤镄勪綋楠,涓鏂归溃绻佸嶅氩彉镄勬搷浣滆儗钖庢湁镌璁稿氩紑鍙戞妧宸у拰闅鹃樸傝繖閲屽垎浜涓涓闀挎寜鍦板浘銮峰彇浣岖疆淇℃伅镄勬坠锷裤 涓轰简镟存槑浜,鍏堜笂涓灞旷ず鏁堟灉:闀挎寜鍦板浘镆愮偣鏄剧ず璇ョ偣鍦扮悊浣岖疆淇℃伅锷熻兘 阃氲繃鏋勯犱竴涓猯ocationSelectOverlay绫绘潵瀹氢箟璇ュ姛鑳,鍦ㄥ湴锲句笂瀵归暱鎸夋坠锷胯繘琛岀洃钖,涓镞︽湁杩欎釜浜嬩欢鍙戠敓灏辫皟鐢╣etAddressFromServer()鏂规硶𨱒ユ樉绀哄湴鍧淇℃伅銆 鍦ㄨュ伐绋嬩腑鍒嗗埆瀹氢箟4涓绫籰ongPressMap.java,locationSelectOverlay.java,popUpPanel.java,Constants.java longPressMap.java涓烘樉绀轰竴涓鍦板浘绫,阃氲繃瀹炰緥鍖栦竴涓猯ocationSelectOverlay绫诲疄鐜伴暱鎸夊湴锲炬樉绀哄湴鐞嗕綅缃淇℃伅锷熻兘浠g爜濡备笅: //longPressMap 绫荤户镓萦apActivity瀵筸apview璧勬簮杩涜岀$悊 public class longPressMap extends MapActivity { private MapView mMapView; locationSelectOverlay mSelectLay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //浣跨敤setContentView鏂规硶璋幂敤R.layout.activity_regeocoder甯冨眬鏂囦欢,鏄剧ず鍦板浘 setContentView(R.layout.geocoder); //銮峰彇鍦板浘瑙嗗浘镄刬d,璧嫔肩粰mMapView mMapView = ((MapView) findViewById(R.id.geocode_MapView)); // 璁剧疆钖鐢ㄥ唴缃镄勭缉鏀炬带浠 mMapView.setBuiltInZoomControls(true); //瀹炰緥鍖栦竴涓猯ocationSelectOverlay绫 mSelectLay = new locationSelectOverlay(this, mMapView, new popUpPanel(this, mMapView)); //灏呜ュ姛鑳藉姞杞藉埌姝ゅ湴锲句笂,钖鐢ㄩ暱鎸夊湴锲炬樉绀鸿ョ偣鍦板潃淇℃伅镄勫姛鑳 mMapView.getOverlays().add(mSelectLay); } } 澶嶅埗浠g爜 locationSelectOverlay绀轰緥浠g爜濡备笅: //locationSelectOverlay绫荤户镓缢verlay鎺ュ彛,瀹炵幇OnGestureListener镓嫔娍鐩戝惉 public class locationSelectOverlay extends Overlay implements OnGestureListener { public popUpPanel mTipPanel; //澹版槑涓涓寮瑰嚭妗嗗硅薄 GeoPoint mSelectPoint; //澹版槑涓涓鍦扮悊鍧愭爣镣瑰硅薄 MapView mMapView; //澹版槑涓涓鍦板浘瑙嗗浘瀵硅薄 Context mContext; //娲诲姩瀵硅薄 TextView mTipText=null; //澹版槑涓涓鏂囨湰瀵硅薄 private static String nearbystr=""; private GestureDetector gestureScanner; //澹版槑涓涓镓嫔娍鐩戝惉瀵硅薄 privateGeocoder coder; //澹版槑涓涓阃嗗湴鐞嗙紪镰佸硅薄 private String addressName=""; //澹版槑涓涓鍦板潃钖岖О瀛楃︿覆 //闀挎寜鍦板浘镆愮偣銮峰彇淇℃伅镄勬瀯阃犲嚱鏁般 public locationSelectOverlay(Activity context,MapView mapView,popUpPanel panel) { this.mContext=context; this.mMapView=mapView; this.mTipPanel=panel; gestureScanner = new GestureDetector(this); //澹版槑涓涓镓嫔娍鐩戝惉瀵硅薄 coder = new Geocoder(context); //澹版槑涓涓阃嗗湴鐞嗙紪镰佸硅薄 } //鐢℉andler鍑芥暟澶勭悊浼犻掓潵镄勫湴鍧淇℃伅,鏄剧ず鍦ㄦ枃链妗嗕腑 private Handler mGeocoderHandler = new Handler() { public void handleMessage(Message msg) { //濡傛灉链夊湴鍧淇℃伅镄勬秷鎭鍙戦佽繃𨱒,灏嗘枃链妗嗕腑璁剧疆涓鸿ュ湴鍧淇℃伅 if(msg.what == Constants.REOCODER_RESULT) { if(mTipText!=null) mTipText.setText(addressName); } //濡傛灉鏄剧ず阌栾,鍒欐枃链妗嗕腑璁剧疆鎶ラ敊淇℃伅 else if(msg.what == Constants.ERROR) { Toast.makeText(mContext, "銮峰彇鍦板潃澶辫触,璇烽吨璇", Toast.LENGTH_SHORT).show(); removeTipPanel(); } } }; //鏄剧ず寮瑰嚭绐楀彛 public boolean showTap(GeoPoint p) { View view = mTipPanel.getView(); mMapView.removeView(view); //甯冨眬鍙傛暟璁剧疆 MapView.LayoutParams geoLP = new MapView.LayoutParams( MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, p, MapView.LayoutParams.BOTTOM_CENTER); //寮瑰嚭绐楀彛镄勬枃链鏄剧ず mTipText = (TextView) view.findViewById(R.id.GeoName); mTipText.setText("姝e湪锷犺浇鍦板潃..."); mTipText.setOnClickListener(new OnClickListener() { public void onClick(View v) { } }); //鍦ㄥ湴锲捐嗗浘涓婃坊锷犺ュ脊鍑虹獥鍙h嗗浘 mMapView.addView(view, geoLP); return false; } //浠庣粡绾搴﹀潗镙囩偣銮峰彇瀵瑰簲镄勫湴鍧淇℃伅 publicvoid getAddressFromServer(final GeoPoint point,final Handler handler) { //澹版槑涓涓绾跨▼ new Thread(){ public void run() { try { // 阃嗗湴鐞嗙紪镰乬etFromLocation()鍑芥暟銮峰彇璇ョ偣瀵瑰簲镄勫墠3涓鍦板潃淇℃伅 ListAddress address = coder.getFromLocation((double)point.getLatitudeE6()/1E6, (double)point.getLongitudeE6()/1E6, 3); if (address != null) { //銮峰彇绗涓涓鍦板潃淇℃伅 Address addres = address.get(0); addressName = ""; if(addres.getAdminArea()!=null) addressName+=addres.getAdminArea(); if(addres.getSubLocality()!=null) addressName += addres.getSubLocality(); if(addres.getFeatureName()!=null) addressName += addres.getFeatureName(); addressName += "闄勮繎"; handler.sendMessage(Message .obtain(handler, Constants.REOCODER_RESULT)); } } catch (AMapException e) { // TODO Auto-generated catch block handler.sendMessage(Message .obtain(handler, Constants.ERROR)); } } }.start(); //绾跨▼钖锷 } //绉昏蛋寮瑰嚭绐楀彛 public void removeTipPanel() { View view = mTipPanel.getView(); mMapView.removeView(view); } //銮峰彇镓嫔娍镎崭綔 public boolean onTouchEvent(MotionEvent event, MapView mapView) { return gestureScanner.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return false; } //闀挎寜鍦板浘,寮瑰嚭鎻愮ず妗,鏄剧ず璇ョ偣鍦板潃淇℃伅 @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub int x = (int)e.getX(); int y = (int)e.getY(); mSelectPoint = mMapView.getProjection().fromPixels(x, y); //璋幂敤鏄剧ず鎻愮ず妗嗗嚱鏁 showTap(mSelectPoint); //璋幂敤浠庣粡绾搴︾偣銮峰彇鍦板潃淇℃伅鍑芥暟 getAddressFromServer(mSelectPoint,mGeocoderHandler); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } 澶嶅埗浠g爜 pouUpPanel瀹氢箟浜嗗脊鍑虹獥鍙g被 public class popUpPanel { private boolean isVisible = false; private MapView mMapView; private View popup; public popUpPanel(Activity paramActivity, MapView paramMapView) { this.mMapView = paramMapView; ViewGroup localViewGroup = (ViewGroup)this.mMapView.getParent(); //璁剧疆寮瑰嚭镄勮嗗浘鏄痠d涓篟.layout.activity_long_press_map镄勮嗗浘 this.popup = paramActivity.getLayoutInflater().inflate(R.layout.activity_long_press_map, localViewGroup, false); 钬 澶嶅埗浠g爜 Constants 瀹氢箟浜嗕紶阃掔殑甯搁噺瀵瑰簲镄勫硷纴濡俻ublic static finalint REOCODER_RESULT=3000; 琛ㄧず阃嗗湴鐞嗙紪镰佺粨鏋滃父閲忥纴public staticfinal int ERROR=1001; 琛ㄧず鍑虹幇阌栾甯搁噺銆