編程gps
❶ 手機android編程,如何調用GPS定位模塊
GPS調用是Android系統中非常重要的一個功能,可以為手機app提供與位置相關的所有功能。
Android編程時,對於GPS模塊有一個專門的管理類,稱為:LocationManager,用來管理與GPS定位服務相關的所有介面。如果您還是不能實現GPS定位模塊的調用,可以到SKYLAB網站上了解下GPS模塊的相關資料,希望能夠幫助到您。
❷ android編程怎麼把GPS坐標轉換為百度地圖坐標
/**
* 標準的GPS經緯度坐標直接在地圖上繪制會有偏移,這是測繪局和地圖商設置的加密,要轉換成網路地圖坐標
*
* @return 網路地圖坐標
*/
publicGeoPoint gpsToBai(String data) {//data格式 nmea標准數據 ddmm.mmmmm,ddmm.mmmm 如3030.90909,11449.1234
String[] p = data.split(",");
intlat = (int) (((int) (Float.valueOf(p[0]) /100) + (100* (Float//將ddmm.mmmm格式轉成dd.ddddd
.valueOf(p[0]) /100.0- (int) (Float.valueOf(p[0]) /100)) /60.0)) * 1E6);
intlon = (int) (((int) (Float.valueOf(p[1]) /100) + (100* (Float
.valueOf(p[1]) /100.0- (int) (Float.valueOf(p[1]) /100)) /60.0)) * 1E6);
GeoPoint pt =newGeoPoint(lat, lon);
returnCoordinateConvert.fromWgs84ToBai(pt);//轉成網路坐標
}
❸ 如何編程實現開啟或關閉GPS
在Activity中添加如下方法:
[java] view
plainprint?
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">private</SPAN> <SPAN style="COLOR: #000066; FONT-WEIGHT: bold">void</SPAN> toggleGPS<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN>
Intent gpsIntent <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #000000; FONT-WEIGHT: bold">new</SPAN> Intent<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
gpsIntent.<SPAN style="COLOR: #006633">setClassName</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"com.android.settings"</SPAN>,
<SPAN style="COLOR: #0000ff">"com.android.settings.widget.SettingsAppWidgetProvider"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
gpsIntent.<SPAN style="COLOR: #006633">addCategory</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"android.intent.category.ALTERNATIVE"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
gpsIntent.<SPAN style="COLOR: #006633">setData</SPAN><SPAN style="COLOR: #009900">(</SPAN>Uri.<SPAN style="COLOR: #006633">parse</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"custom:3"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">try</SPAN> <SPAN style="COLOR: #009900">{</SPAN>
PendingIntent.<SPAN style="COLOR: #006633">getBroadcast</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #000000; FONT-WEIGHT: bold">this</SPAN>, <SPAN style="COLOR: #cc66cc">0</SPAN>, gpsIntent, <SPAN style="COLOR: #cc66cc">0</SPAN><SPAN style="COLOR: #009900">)</SPAN>.<SPAN style="COLOR: #006633">send</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #009900">}</SPAN>
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">catch</SPAN> <SPAN style="COLOR: #009900">(</SPAN>CanceledException e<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN>
e.<SPAN style="COLOR: #006633">printStackTrace</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #009900">}</SPAN>
<SPAN style="COLOR: #009900">}</SPAN>
private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
}
catch (CanceledException e) {
e.printStackTrace();
}
}
這個方法是1個純開關,如果當前是開啟的,那麼就會關閉它,反之亦然。
檢查GPS開關狀態
那麼,如何查看當前的GPS開關狀態呢?可以用上面提到的反射方式調用isLocationProviderEnabled,代碼片斷如下:
[java] view
plainprint?
secureClass <SPAN style="COLOR: #339933">=</SPAN> cl.<SPAN style="COLOR: #006633">loadClass</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"android.provider.Settings$Secure"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
isMethod <SPAN style="COLOR: #339933">=</SPAN> secureClass.<SPAN style="COLOR: #006633">getMethod</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"isLocationProviderEnabled"</SPAN>,
ContentResolver.<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">class</SPAN>, <SPAN style="COLOR: #003399">String</SPAN>.<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">class</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #003399">Boolean</SPAN> ret <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #003399">Boolean</SPAN><SPAN style="COLOR: #009900">)</SPAN> isMethod.<SPAN style="COLOR: #006633">invoke</SPAN><SPAN style="COLOR: #009900">(</SPAN>secureClass, <SPAN style="COLOR: #000000; FONT-WEIGHT: bold">this</SPAN>
.<SPAN style="COLOR: #006633">getContentResolver</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN>, <SPAN style="COLOR: #0000ff">"gps"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
secureClass = cl.loadClass("android.provider.Settings$Secure");
isMethod = secureClass.getMethod("isLocationProviderEnabled",
ContentResolver.class, String.class);
Boolean ret = (Boolean) isMethod.invoke(secureClass, this
.getContentResolver(), "gps");
也可以直接用下面的方法:
[java] view
plainprint?
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">private</SPAN> <SPAN style="COLOR: #000066; FONT-WEIGHT: bold">void</SPAN> isGPSEnable<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN>
<SPAN style="FONT-STYLE: italic; COLOR: #666666">/* 用Setting.System來讀取也可以,只是這是更舊的用法
String str = Settings.System.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
*/</SPAN>
<SPAN style="COLOR: #003399">String</SPAN> str <SPAN style="COLOR: #339933">=</SPAN> Settings.<SPAN style="COLOR: #006633">Secure</SPAN>.<SPAN style="COLOR: #006633">getString</SPAN><SPAN style="COLOR: #009900">(</SPAN>getContentResolver<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN>,
Settings.<SPAN style="COLOR: #006633">Secure</SPAN>.<SPAN style="COLOR: #006633">LOCATION_PROVIDERS_ALLOWED</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
Log.<SPAN style="COLOR: #006633">v</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"GPS"</SPAN>, str<SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">if</SPAN> <SPAN style="COLOR: #009900">(</SPAN>str <SPAN style="COLOR: #339933">!=</SPAN> <SPAN style="COLOR: #000066; FONT-WEIGHT: bold">null</SPAN><SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN>
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">return</SPAN> str.<SPAN style="COLOR: #006633">contains</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #0000ff">"gps"</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #009900">}</SPAN>
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">else</SPAN><SPAN style="COLOR: #009900">{</SPAN>
<SPAN style="COLOR: #000000; FONT-WEIGHT: bold">return</SPAN> <SPAN style="COLOR: #000066; FONT-WEIGHT: bold">false</SPAN><SPAN style="COLOR: #339933">;</SPAN>
<SPAN style="COLOR: #009900">}</SPAN>
<SPAN style="COLOR: #009900">}</SPAN>
private void isGPSEnable() {
/* 用Setting.System來讀取也可以,只是這是更舊的用法
String str = Settings.System.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
*/
String str = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.v("GPS", str);
if (str != null) {
return str.contains("gps");
}
else{
return false;
}
}
這2種方法的原理都是一樣的,方法2其實也就是isLocationProviderEnabled實際調用的代碼,只是Google未對讀取操作進行許可權限制。
❹ C語言編程:GPS數據處理
這是通過online judge測試的
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int check(char *);
void utc(char *);
char bjtime[7];
int main()
{
char a[200];
int f=0;
char *ptr;
do
{
scanf("%s",a);
if(strncmp("$GPRMC",a,6)==0)
{
ptr=a;
for(int i=0;i<2;i++){
ptr=strchr(ptr,',');
ptr++;
}
if (ptr[0]=='A' && check(a))
{
utc(a);
f=1;
}
}
}while(strcmp("END",a)!=0);
if (f) printf("%c%c:%c%c:%c%c",bjtime[0],bjtime[1],bjtime[2],bjtime[3],bjtime[4],bjtime[5]);
return 0;
}
int check(char *s)
{
char *ck,*str;
int i;
char m=s[1];
for(i=2;i<strlen(s)-3;i++)
m^=s[i];
int z=(int)m%65536;
printf("%0x",z);
ck=&s[strlen(s)-2];
int x=strtol(ck,&str,16);
if(x==z) return 1;
else return 0;
}
void utc(char * s)
{
char *str1;
char str[7];
str1=&s[7];
strncpy(bjtime,str1,6);
int x=strtol(bjtime,&str1,10);
x+=80000;
x%=240000;
for(int i=5;i>=0;i--){
bjtime[i]=(char)((int)'0'+x%10);
x/=10;
}
}
❺ GPS定位 編程用匯編語言實現
首先, 如果不是非要用匯編的話, 就不要用匯編,因為中間牽涉到很多字元串的處理。
不過看你必須在8088上完成, 估計就是要求用匯編了。
簡單的說下,首先你要實現用8088接受串口數據。 這應該不難,教科書上有。
然後去了解GPS數據格式,GPS數據是以字元串的形式一串串的輸出的。 每一串都有個標示頭,必須要查找相關資料,網路上很多的。
GPS輸出的字元串中有些是包含有經緯度的。比如:
"#ABCD asdf, 0, 23, 45.564, 52.2356, 23, 4"
其中#ABCD是標示頭, 45.564是維度, 52.2356是經度。
那麼你的程序首先要找到標示頭, 然後在找到經緯度的在字元串的位子, 比如可以用簡單的數逗號來找。
剩下的就是顯示問題了。
❻ 如何利用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的例子。
{
publicvoidonCreate(Bundleparams){
super.onCreate(params);
setContentView(R.layout.main);
}
publicbooleanonKeyDown(intkeyCode,KeyEventevent){
returntrue;
}
}
三 View類
View類是Android的一個超類,這個類幾乎包含了所有的屏幕類型。但它們之間有一些不同。每一個view都有一個用於繪畫的畫布。這個畫布可以用 來進行任意擴展。本文為了方便起見,只涉及到了兩個主要的View類型:定義View和Android的XML內容View。在上面的代碼中,使用的是 「Hello World」 XML View,它是以非常自然的方式開始的。
如果我們查看一下新的Android工程,就會發現一個叫main.xml的文件。在這個文件中,通過一個簡單的XML文件,描述了一個屏幕的布局。這個 簡單的xml文件的內容如下:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/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=""
/>
</RelativeLayout>
上面的內容的功能看起來非常明顯。這個特殊文件定義了一個相關的布局,這就意味著通過一個元素到另一個元素的關系或是它們父元素的關系來描述。對於視圖來 說,有一些用於布局的方法,但是在本文中只關注於上述的xml文件。
RealtiveLayout中包含了一個填充整個屏幕的文本框(也就是我們的LocateMe activity)。這個LocateMe activity在默認情況下是全屏的,因此,文本框將繼承這個屬性,並且文本框將在屏幕的左上角顯示。另外,必須為這個XML文件設置一個引用數,以便 Android可以在源代碼中找到它。在默認情況下,這些引用數被保存在R.java中,代碼如下:
publicfinalclassR{
publicstaticfinalclasslayout{
publicstaticfinalintmain=0x7f030001;
}
}
視圖也可以被嵌套,但和J2ME不同,我們可以將定製的視圖和Android團隊發布的Widgets一起使用。在J2ME中,開發人員被迫選擇 GameCanvas和J2ME應用程序畫布。這就意味著如果我們想要一個定製的效果,就必須在GameCanvas上重新設計我們所有的widget。 Android還不僅僅是這些,視圖類型也可以混合使用。Android還帶了一個 widget庫,這個類庫包括了滾動條,文本實體,進度條以及其他很多控制項。這些標準的widget可以被重載或被按著我們的習慣定製。現在讓我們來進入 我們的例子。
四、Android實例
這個演示應用程序將演示了用戶的當前的經度和緯度(在文本框中顯示)。onCreate構造方法將和上面的例子基本相同,除了在其中加入了鍵盤處理,現在 讓我們看一下onKeyDown的代碼。
publicbooleanonKeyDown(intkeyCode,KeyEventevent){
if(keyCode!=KeyEvent.KEYCODE_DPAD_CENTER||m_bLoading)
{
returntrue;
}
m_bLoading=true;
getLocation();
returntrue;
}
下面讓我們來解釋一下這段代碼,首先,這段代碼檢查了當前被按下的鍵,但還沒有開始處理。而是在getLocation方法中處理這一切的。然後,將裝載 flag標志以及調用getLocation方法,下面是getLocation方法的代碼。
privatevoidgetLocation(){
Locationloc;
LocationManagerlocMan;
LocationProviderlocPro;
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方法列出了上面看到的內容。
publicvoidCreateView(){
customView=newCustomView(this);
}
這個方法簡單地調用了CustomView對象的構造方法。CustomView類的定義如下:
{
LocateMeoverlord;
publicCustomView(LocateMepCtx){
super(pCtx);
overlord=pCtx;
}
publicvoidonDraw(Canvascvs){
Paintp=newPaint();
StringsLat="Latitude:"+overlord.getLat();
StringsLon="Longitude:"+overlord.getLon();
cvs.drawText(sLat,32,32,p);
cvs.drawText(sLon,32,44,p);
}
}
這個定製的Android視圖獲得了經度和違度的測試數據,並將這些數據顯示在屏幕上。這要求一個指向LocateMe的指針,Activity類是整 個應用程序的核心。它的兩個方法是構造方法和onDraw方法。這個構造方法調用了超類的構造方法以及引起了Activity指針的中斷。onDraw方 法將建立一個新的Paint對象(這個對象封裝了顏色、透明度以及其他的主題信息),這個對象將會訪問顏色主題。在本程序中,安裝了用於顯示的字元串,並 使用畫布指針將它們畫到屏幕上。這個和我們了解的J2ME游戲的畫布看起來非常類似。
六、Android展望
從純粹的開發觀點看,Android是一個非常強大的SDK。它使用基於XML的布局和定製視圖聯合了起來。並可以使用滾動條、地圖以及其他的組件。所以 的這一切都可以被重載,或由開發人員來定製。但它所提供的文檔非常粗糙。在文檔中並沒有象SMS等技術,但是從整體上來看Android SDK,還是非常有希望的。也非常符合Google承諾的「First Look」SDK。現在我們要做的就是等待Google發布第一個基於Android的手機,並使用它。
如若滿意,請點擊右側【採納答案】,如若還有問題,請點擊【追問】
希望我的回答對您有所幫助,望採納!
~O(∩_∩)O~