首页 > 代码库 > android简单定位和画地图

android简单定位和画地图

在讲解代码前,首先配置AndroidManifest.xml

以下权限貌似是缺一不可哦:

   <!-- 访问网络,网络定位需要上网 -->    <uses-permission android:name="android.permission.INTERNET" />    <!-- 这个权限用于访问GPS定位 -->    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <!-- 这个权限用于进行网络定位 -->    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <!-- 用于读取手机当前的状态 -->    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <!-- 修改删除launcher.db内容权限: -->    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

接着添加:

     <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote" >            <intent-filter>                <action android:name="com.baidu.location.service_v2.2" />            </intent-filter>        </service>        <meta-data             android:name="com.baidu.lbsapi.API_KEY"            android:value="ersHTSRLiYb2TRMTcdi3zv23"/>
注意:<intent-filter> 2.2以上的版本已经支持,可以不写;API_KEY需要到百度地图LBS开发平台的API控制台创建应用获得

同时还需要下载BaiduLBS_AndroidSDK_Lib.zip,解压后将libs下的所有文件添加到自己工程下的libs里
这样配置工作已经做好了,下面的是activity里的代码:
 1 package com.ghp.practice6_dingwei; 2  3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7 import com.baidu.location.LocationClientOption.LocationMode; 8 import com.baidu.nplatform.comapi.map.OverlayItem.CoordType; 9 10 import android.app.Activity;11 import android.content.Intent;12 import android.os.Bundle;13 import android.view.Menu;14 import android.view.MenuItem;15 import android.view.View;16 import android.view.View.OnClickListener;17 import android.widget.Button;18 import android.widget.TextView;19 20 public class MainActivity extends Activity implements OnClickListener,BDLocationListener {21     TextView adress;22     Button get_adress;23     private LocationClient locationClient;24     TextView mapview;25     @Override26     protected void onCreate(Bundle savedInstanceState) {27         super.onCreate(savedInstanceState);28         setContentView(R.layout.activity_main);29         30         locationClient=new LocationClient(getApplicationContext());31         locationClient.registerLocationListener(this);32         initLocation();33         initView();34     }35 36     public void initLocation() {37         LocationClientOption option=new LocationClientOption();38         option.setIsNeedAddress(true);//不设置地址会为null39         option.setLocationMode(LocationMode.Hight_Accuracy);//定位模式40         option.setTimeOut(3000);41         option.setCoorType("gcj02");//坐标系42         locationClient.setLocOption(option);43     }44     public void initView() {45         adress=(TextView) findViewById(R.id.adress);46         get_adress=(Button) findViewById(R.id.get_adress);47         get_adress.setOnClickListener(this);48         mapview=(TextView) findViewById(R.id.mapview);49         mapview.setOnClickListener(this);50     }51     @Override52     public void onClick(View v) {53         // TODO Auto-generated method stub54         switch (v.getId()) {55         case R.id.get_adress:56             locationClient.start();57             break;58         case R.id.mapview:59             Intent intent=new Intent(this,MapViewActivity.class);60             startActivity(intent);61             finish();62             break;63 64         default:65             break;66         }67         68     }69     @Override70     public void onReceiveLocation(BDLocation location) {71         // TODO Auto-generated method stub72         StringBuffer sb=new StringBuffer();73         double latitude=location.getLatitude();74         double longitude=location.getLongitude();75         String addr=location.getAddrStr();76         String city=location.getCity();77         String street=location.getStreet();78         String district=location.getDistrict();79         String province=location.getProvince();80         String streetNumber=location.getStreetNumber();        81         int errorCode=location.getLocType();82         sb.append("经度:"+latitude);83         sb.append("纬度:"+longitude);84         sb.append("地址:"+addr);85         sb.append("errorCode:"+errorCode);86         //单独查看87         sb.append("\n省份:"+province);88         sb.append("\n城市:"+city);                89         sb.append("\n区:"+district);90         sb.append("\n街道:"+street);    91         sb.append("\n街道号:"+streetNumber);92         adress.setText(sb.toString());93     }94 }

展示地图MapViewActivity:

 1 package com.ghp.practice6_dingwei; 2  3 import com.baidu.mapapi.SDKInitializer; 4 import com.baidu.mapapi.map.BaiduMapOptions; 5 import com.baidu.mapapi.map.MapStatus; 6 import com.baidu.mapapi.map.MapView; 7 import com.baidu.mapapi.model.LatLng; 8  9 import android.app.Activity;10 import android.os.Bundle;11 import android.view.Menu;12 import android.view.MenuItem;13 14 public class MapViewActivity extends Activity {15     private MapView mapView;16     @Override17     protected void onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         //setContentView(R.layout.activity_map_view);20         SDKInitializer.initialize(super.getApplicationContext());21         double x=37;22         double y=121;23         LatLng latlng=new LatLng(x, y);24         MapStatus status=new MapStatus.Builder().target(latlng).zoom(13).build();25         BaiduMapOptions options=new BaiduMapOptions();        26         options.mapStatus(status);//状态设置27         options.zoomControlsEnabled(true);//缩放        28         mapView=new MapView(this,options);29         setContentView(mapView);//加载view30     }31 32     @Override33     protected void onDestroy() {34         // TODO Auto-generated method stub35         super.onDestroy();36         mapView.onDestroy();37     }38 39     @Override40     protected void onPause() {41         // TODO Auto-generated method stub42         super.onPause();43         mapView.onPause();44     }45     46 }

布局文件:

 1 <RelativeLayout 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     tools:context="${relativePackage}.${activityClass}" > 6  7    <com.baidu.mapapi.map.MapView 8         android:id="@+id/mapview" 9         android:layout_width="match_parent"10         android:layout_height="match_parent">       11     </com.baidu.mapapi.map.MapView>12 13 </RelativeLayout>

 

android简单定位和画地图