當前位置:首頁 » 安卓系統 » android載入地圖api

android載入地圖api

發布時間: 2024-09-24 07:04:55

㈠ 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; 琛ㄧず鍑虹幇閿欒甯擱噺銆

熱點內容
取消win10開機密碼怎麼設置 發布:2024-11-24 15:26:03 瀏覽:796
編譯原理for循環三地址碼 發布:2024-11-24 15:16:44 瀏覽:201
php2048 發布:2024-11-24 15:14:49 瀏覽:894
php旋轉圖片 發布:2024-11-24 15:03:48 瀏覽:534
提新車如何查看車輛配置 發布:2024-11-24 14:58:40 瀏覽:889
編譯期多態與運行期多態 發布:2024-11-24 14:47:07 瀏覽:580
sqlserver連接工具 發布:2024-11-24 14:24:51 瀏覽:293
怎麼排除安卓軟體不兼容 發布:2024-11-24 14:24:18 瀏覽:526
怎麼讓Win10運行安卓 發布:2024-11-24 14:23:12 瀏覽:324
什麼是設置伺服器怎麼弄 發布:2024-11-24 14:01:59 瀏覽:334