1.å¦ä½å©ç¨Androidç¼ç¨å®ç°GPSå®ä½
2.android开发怎么获得手机的定位定位gps?
å¦ä½å©ç¨Androidç¼ç¨å®ç°GPSå®ä½
æ¨å¥½ï¼å¾é«å ´ä¸ºæ¨è§£çãä¸ãåå¤å·¥ä½
éè¦å¦ä¸ä¸ç§è½¯ä»¶ï¼
1. Eclipse
2. Android SDK
3. å¼åAndroidç¨åºçEclipse æ件
为äºå¼å§æ们çå·¥ä½ï¼é¦å è¦å®è£ Eclipseï¼ç¶åä»Googleçç½ç«è·å¾Android SDKï¼å¹¶ä¸å®è£ Eclipseæ件ã
äºãActivityç±»
æ¯ä¸ç§ç§»å¨å¼åç¯å¢é½æèªå·±çåºç±»ãå¦J2MEåºç¨ç¨åºçåºç±»æ¯midletsï¼BREWçåºç±»æ¯appletsï¼èAndroidç¨åºçåºç±»æ¯ Activityãè¿ä¸ªactivity为æ们æä¾äºå¯¹ç§»å¨æä½ç³»ç»çåºæ¬åè½åäºä»¶ç访é®ãè¿ä¸ªç±»å å«äºåºæ¬çæé æ¹æ³ï¼é®çå¤çï¼æèµ·æ¥æ¢å¤åè½ï¼ä»¥ åå ¶ä»åºå±çææ设å¤ç访é®ãå®è´¨ä¸ï¼æ们çåºç¨ç¨åºå°æ¯ä¸ä¸ªActivityç±»çæ©å±ãå¨æ¬æä¸è¯»è å°ä¼éè¿ä¾åå¦ä¹ å°å¦ä½ä½¿ç¨Activityç±»æ¥ç¼ åAndroidç¨åºãä¸é¢æ¯ä¸ä¸ªç®åç继æ¿Activityçä¾åã
public class LocateMe extends Activity{public void onCreate(Bundle params){
super.onCreate(params);
setContentView(R.layout.main);
}
public boolean onKeyDown(int keyCode, KeyEvent event){
return true;
}
}
ä¸ Viewç±»
Viewç±»æ¯Androidçä¸ä¸ªè¶ ç±»ï¼è¿ä¸ªç±»å ä¹å å«äºææçå±å¹ç±»åãä½å®ä»¬ä¹é´æä¸äºä¸åãæ¯ä¸ä¸ªviewé½æä¸ä¸ªç¨äºç»ç»çç»å¸ãè¿ä¸ªç»å¸å¯ä»¥ç¨ æ¥è¿è¡ä»»ææ©å±ãæ¬æ为äºæ¹ä¾¿èµ·è§ï¼åªæ¶åå°äºä¸¤ä¸ªä¸»è¦çViewç±»åï¼å®ä¹ViewåAndroidçXMLå 容Viewãå¨ä¸é¢ç代ç ä¸ï¼ä½¿ç¨çæ¯ âHello Worldâ XML Viewï¼å®æ¯ä»¥é常èªç¶çæ¹å¼å¼å§çã
å¦ææ们æ¥çä¸ä¸æ°çAndroidå·¥ç¨ï¼å°±ä¼åç°ä¸ä¸ªå«main.xmlçæ件ãå¨è¿ä¸ªæ件ä¸ï¼éè¿ä¸ä¸ªç®åçXMLæ件ï¼æè¿°äºä¸ä¸ªå±å¹çå¸å±ãè¿ä¸ª ç®åçxmlæ件çå 容å¦ä¸ï¼
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHoriz
android:text="ress the center key to locate yourself"
/>
</RelativeLayout>
ä¸é¢çå 容çåè½çèµ·æ¥é常ææ¾ãè¿ä¸ªç¹æ®æ件å®ä¹äºä¸ä¸ªç¸å ³çå¸å±ï¼è¿å°±æå³çéè¿ä¸ä¸ªå ç´ å°å¦ä¸ä¸ªå ç´ çå ³ç³»ææ¯å®ä»¬ç¶å ç´ çå ³ç³»æ¥æè¿°ã对äºè§å¾æ¥ 说ï¼æä¸äºç¨äºå¸å±çæ¹æ³ï¼ä½æ¯å¨æ¬æä¸åªå ³æ³¨äºä¸è¿°çxmlæ件ã
RealtiveLayoutä¸å å«äºä¸ä¸ªå¡«å æ´ä¸ªå±å¹çææ¬æ¡ï¼ä¹å°±æ¯æ们çLocateMe activityï¼ãè¿ä¸ªLocateMe activityå¨é»è®¤æ åµä¸æ¯å ¨å±çï¼å æ¤ï¼ææ¬æ¡å°ç»§æ¿è¿ä¸ªå±æ§ï¼å¹¶ä¸ææ¬æ¡å°å¨å±å¹çå·¦ä¸è§æ¾ç¤ºãå¦å¤ï¼å¿ 须为è¿ä¸ªXMLæ件设置ä¸ä¸ªå¼ç¨æ°ï¼ä»¥ä¾¿ Androidå¯ä»¥å¨æºä»£ç ä¸æ¾å°å®ãå¨é»è®¤æ åµä¸ï¼è¿äºå¼ç¨æ°è¢«ä¿åå¨R.javaä¸ï¼ä»£ç å¦ä¸ï¼
public final class R{public static final class layout{
public static final int main=0x7f;
}
}
è§å¾ä¹å¯ä»¥è¢«åµå¥ï¼ä½åJ2MEä¸åï¼æ们å¯ä»¥å°å®å¶çè§å¾åAndroidå¢éåå¸çWidgetsä¸èµ·ä½¿ç¨ãå¨J2MEä¸ï¼å¼å人å被迫éæ© GameCanvasåJ2MEåºç¨ç¨åºç»å¸ãè¿å°±æå³çå¦ææ们æ³è¦ä¸ä¸ªå®å¶çææï¼å°±å¿ é¡»å¨GameCanvasä¸éæ°è®¾è®¡æ们ææçwidgetã Androidè¿ä¸ä» ä» æ¯è¿äºï¼è§å¾ç±»åä¹å¯ä»¥æ··å使ç¨ãAndroidè¿å¸¦äºä¸ä¸ª widgetåºï¼è¿ä¸ªç±»åºå æ¬äºæ»å¨æ¡ï¼ææ¬å®ä½ï¼è¿åº¦æ¡ä»¥åå ¶ä»å¾å¤æ§ä»¶ãè¿äºæ åçwidgetå¯ä»¥è¢«éè½½æ被æçæ们çä¹ æ¯å®å¶ãç°å¨è®©æ们æ¥è¿å ¥ æ们çä¾åã
åãAndroidå®ä¾
è¿ä¸ªæ¼ç¤ºåºç¨ç¨åºå°æ¼ç¤ºäºç¨æ·çå½åçç»åº¦å纬度ï¼å¨ææ¬æ¡ä¸æ¾ç¤ºï¼ãonCreateæé æ¹æ³å°åä¸é¢çä¾ååºæ¬ç¸åï¼é¤äºå¨å ¶ä¸å å ¥äºé®çå¤çï¼ç°å¨ 让æ们çä¸ä¸onKeyDownç代ç ã
public boolean onKeyDown(int keyCode, KeyEvent event){if(keyCode != KeyEvent.KEYCODE_DPAD_CENTER || m_bLoading)
{
return true;
}
m_bLoading = true;
getLocation();
return true;
}
ä¸é¢è®©æ们æ¥è§£éä¸ä¸è¿æ®µä»£ç ï¼é¦å ï¼è¿æ®µä»£ç æ£æ¥äºå½å被æä¸çé®ï¼ä½è¿æ²¡æå¼å§å¤çãèæ¯å¨getLocationæ¹æ³ä¸å¤çè¿ä¸åçãç¶åï¼å°è£ è½½ flagæ å¿ä»¥åè°ç¨getLocationæ¹æ³ï¼ä¸é¢æ¯getLocationæ¹æ³ç代ç ã
private void getLocation(){Location loc;
LocationManager locMan;
LocationProvider locPro;
List<LocationProvider> proList;
setContentView(R.layout.laoding);
locMan = (LocationManager) getSystemService(LOCATION_SERVICE);
proList = locMan.getProviders();
locPro = proList.get(0);
loc = locMan.getCurrentLocation(locPro.getName());
Lat = (float)loc.getLatitude();
Lon = (float)loc.getLongitude();
CreateView();
setContentView(customView);
}
å°è¿ä¸ºæ¢ï¼ç¨åºå¼å§åå¾æ´æ趣äºãä½æ¯ä¸å¹¸çæ¯ï¼Googleå ³äºä¹æ¹é¢çææ¡£è¿æ¯æ¯è¾å°äºãå¨ç¨åºçåé声æä¹åï¼æ们éè¦æ¼ç¤ºä¸äºè£ 载信æ¯ã R.layout.loading符åäºå¦ä¸ä¸ªç®åçXMLå¸å±è§å¾ãéè¿ç®åå°è°ç¨setContentViewæ¹æ³å¯ä»¥ä½¿ç¨è½¬è½½ä¿¡æ¯éç»å±å¹ã
读è è¦æ³¨æçæ¯ï¼å¨ç¼è¯æ¶ï¼Androidä¼é¢å å°ææçXMLå¸å±æ°æ®å è£ èµ·æ¥ãå¦ææ们æ³å¨ç¼è¯åååå¸å±å±æ§ï¼æçè§å®ï¼æä»¬å¿ é¡»å¨æºç¨åºä¸åè¿äº äºã
è·å¾LocationManagerçå¯ä¸æ¹æ³æ¯éè¿getSystemService()æ¹æ³çè°ç¨ãéè¿ä½¿ç¨LocationManagerï¼ æ们å¯ä»¥è·å¾ä¸ä¸ªä½ç½®æä¾è çå表ãå¨ä¸ä¸ªçå®çææ设å¤ä¸ï¼è¿ä¸ªå表å å«äºä¸äºGPSæå¡ãå®é ä¸ï¼æ们å¸æéæ©æ´å¼ºå¤§ï¼æ´ç²¾ç¡®ï¼æåä¸å¸¦æå ¶ä»éå æ å¡çGPSãç°å¨ï¼å¨æ¨¡æå¨ä¸æä¾äºä¸ä¸ªç¨äºæµè¯çGPSï¼è¿ä¸ªGPSæ¥èªSan Franciscoãå®å¶çGPSæ件å¯ä»¥å¯ä»¥è¢«ä¸ä¼ ï¼å¹¶è¿è¡æµè¯ãå¦ææ们è¦æµè¯æ´å¤æçåºç¨ï¼æ¥èªSan FranciscoçGPSå¯è½å¹¶ä¸éåã
ç®åæ们å¯ä»¥ä½¿ç¨ä½ç½®ç®¡çå¨åä½ç½®æä¾è è¿è¡getCurrentLocationçè°ç¨ãè¿ä¸ªæ¹æ³è¿åæ¬æºçå½åä½ç½®çä¸ä¸ªå¿«ç §ï¼è¿ä¸ªå¿«ç §å°ä»¥ Location对象形å¼æä¾ãå¨ææ设å¤ä¸ï¼æ们å¯ä»¥è·å¾å½åä½ç½®çç»åº¦å纬度ãç°å¨ï¼ä½¿ç¨è¿ä¸ªèæçææ设å¤ï¼æ们å¯ä»¥è·å¾è¿ä¸ªä¾åç¨åºçæç»ç»æï¼ å»ºç«äºæ¾ç¤ºä¸ä¸ªå®å¶çè§å¾ã
äºã使ç¨å®å¶è§å¾
å¨æç®åççªä½ä¸ï¼ä¸ä¸ªAndroidä¸çè§å¾ä» ä» éè¦éè½½ä¸ä¸ªonDrawæ¹æ³ãå®å¶è§å¾å¯ä»¥æ¯å¤æç3Då®ç°ææ¯é常ç®åçææ¬å½¢å¼ãä¸é¢ç CreateViewæ¹æ³ååºäºä¸é¢çå°çå 容ã
public void CreateView(){customView = new CustomView(this);
}
è¿ä¸ªæ¹æ³ç®åå°è°ç¨äºCustomView对象çæé æ¹æ³ãCustomViewç±»çå®ä¹å¦ä¸ï¼
public class CustomView extends View{LocateMe overlord;
public CustomView(LocateMe pCtx){
super(pCtx);
overlord = pCtx;
}
public void onDraw(Canvas cvs){
Paint p = new Paint();
String sLat = "Latitude: " + overlord.getLat();
String sLon = "Longitude: " + overlord.getLon();
cvs.drawText(sLat , , , p);
cvs.drawText(sLon, , , p);
}
}
è¿ä¸ªå®å¶çAndroidè§å¾è·å¾äºç»åº¦åè¿åº¦çæµè¯æ°æ®ï¼å¹¶å°è¿äºæ°æ®æ¾ç¤ºå¨å±å¹ä¸ãè¿è¦æ±ä¸ä¸ªæåLocateMeçæéï¼Activityç±»æ¯æ´ 个åºç¨ç¨åºçæ ¸å¿ãå®ç两个æ¹æ³æ¯æé æ¹æ³åonDrawæ¹æ³ãè¿ä¸ªæé æ¹æ³è°ç¨äºè¶ ç±»çæé æ¹æ³ä»¥åå¼èµ·äºActivityæéçä¸æãonDrawæ¹ æ³å°å»ºç«ä¸ä¸ªæ°çPaint对象ï¼è¿ä¸ªå¯¹è±¡å°è£ äºé¢è²ãéæ度以åå ¶ä»ç主é¢ä¿¡æ¯ï¼ï¼è¿ä¸ªå¯¹è±¡å°ä¼è®¿é®é¢è²ä¸»é¢ãå¨æ¬ç¨åºä¸ï¼å®è£ äºç¨äºæ¾ç¤ºçå符串ï¼å¹¶ 使ç¨ç»å¸æéå°å®ä»¬ç»å°å±å¹ä¸ãè¿ä¸ªåæ们äºè§£çJ2ME游æçç»å¸çèµ·æ¥é常类似ã
å ãAndroidå±æ
ä»çº¯ç²¹çå¼åè§ç¹çï¼Androidæ¯ä¸ä¸ªé常强大çSDKãå®ä½¿ç¨åºäºXMLçå¸å±åå®å¶è§å¾èåäºèµ·æ¥ã并å¯ä»¥ä½¿ç¨æ»å¨æ¡ãå°å¾ä»¥åå ¶ä»çç»ä»¶ãæ以 çè¿ä¸åé½å¯ä»¥è¢«éè½½ï¼æç±å¼å人åæ¥å®å¶ãä½å®ææä¾çææ¡£é常ç²ç³ãå¨ææ¡£ä¸å¹¶æ²¡æ象SMSçææ¯ï¼ä½æ¯ä»æ´ä½ä¸æ¥çAndroid SDKï¼è¿æ¯é常æå¸æçãä¹é常符åGoogleæ¿è¯ºçâFirst LookâSDKãç°å¨æ们è¦åçå°±æ¯çå¾ Googleåå¸ç¬¬ä¸ä¸ªåºäºAndroidçææºï¼å¹¶ä½¿ç¨å®ã
å¦è¥æ»¡æï¼è¯·ç¹å»å³ä¾§ãé纳çæ¡ãï¼å¦è¥è¿æé®é¢ï¼è¯·ç¹å»ã追é®ã
å¸ææçåç对æ¨ææ帮å©ï¼æé纳ï¼
~ O(â©_â©)O~
android开发怎么获得手机的gps?
第一步,申明权限。源码(5.0之后权限需要动态申请,定位定位源码 图片 加logo具体代码和这个问题无关就不贴出来了)<!--定位权限-->
<uses-permissionandroid:name=\"android.permission.ACCESS_FINE_LOCATION\"/>
<uses-permissionandroid:name=\"android.permission.ACCESS_COARSE_LOCATION\"/>
第二步通过LocationManager类获取位置信息,源码下面是定位定位pcm 源码 hdmi一个封装好的工具类
**
*CreatedbyDELLzhanghuirongon/3/.
*获取当前位置信息
*/
publicclassMyLocationUtil{
privatestaticStringprovider;
publicstaticLocationgetMyLocation(){
//获取当前位置信息
//获取定位服务
LocationManagerlocationManager=(LocationManager)MyApp.getContext().getSystemService(Context.LOCATION_SERVICE);
//获取当前可用的位置控制器
List<String>list=locationManager.getProviders(true);
if(list.contains(locationManager.GPS_PROVIDER)){
//GPS位置控制器
provider=locationManager.GPS_PROVIDER;//GPS定位
}elseif(list.contains(locationManager.NETWORK_PROVIDER)){
//网络位置控制器
provider=locationManager.NETWORK_PROVIDER;//网络定位
}
if(provider!=null){
if(ActivityCompat.checkSelfPermission(MyApp.getContext(),Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(MyApp.getContext(),Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
//TODO:Considercalling
//ActivityCompat#requestPermissions
//heretorequestthemissingpermissions,andthenoverriding
//publicvoidonRequestPermissionsResult(intrequestCode,String[]permissions,
//int[]grantResults)
//tohandlethecasewheretheusergrantsthepermission.Seethedocumentation
//forActivityCompat#requestPermissionsformoredetails.
returnnull;
}
LocationlastKnownLocation=locationManager.getLastKnownLocation(provider);
returnlastKnownLocation;
}else{
ToastUtils.makeText(\"请检查网络或GPS是否打开\");
}
returnnull;
}
}
第三步(其实到上一步这个问题已经解决了,这个算扩展吧)将位置信息转换成地址信息。源码mcst源码错误
在高德或者百度地图开发者平台申请访问api许可。定位定位将第二步获取到的源码经纬度信息上传查询对应坐标信息。因为百度和高德用的定位定位不是同一个坐标系,查询时仔细看官方API。源码
直接通过安卓的定位定位原生接口获取一个gps的位置意义不是很大。这个数据在一定的源码坐标系上才有意义。建议去高德的定位定位湖南源码云开发平台注册个帐号,引入sdk来做,源码地理位置与地理位置解析的定位定位文章共享源码概念先了解下吧。
//第一步先获取LocationManager的对象LocationManagerGpsManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);//通过LocationManager的对象来获取到Location的信息。Locationlocation=GpsManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//Location中经常用到的有以下几种:/*location.getAccuracy();精度location.getAltitude();高度:海拔location.getBearing();导向location.getSpeed();速度location.getLatitude();纬度location.getLongitude();经度location.getTime();UTC时间以毫秒计*/注:需要添加使用权限的哦