android手机获取gps和基站的经纬度地址实现代码
程序员文章站
2023-12-05 17:12:52
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffd3d7df"
android:orientation="vertical" >
<linearlayout
android:id="@+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginbottom="20dip"
android:layout_marginleft="20dip"
android:layout_marginright="20dip"
android:layout_margintop="20dip"
android:background="@drawable/bg_frame"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingbottom="2dip"
android:paddingleft="10dip"
android:paddingright="10dip"
android:paddingtop="10dip" >
<textview
android:id="@+id/providertitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取经纬度:"
android:textcolor="#007979" />
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<radiogroup
android:id="@+id/providergroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<radiobutton
android:id="@+id/gpsprovide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="gps"
android:textcolor="#005ab5" />
<radiobutton
android:id="@+id/networkprovide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="基站"
android:textcolor="#005ab5" />
</radiogroup>
<imagebutton
android:id="@+id/bestlocationproid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/loction" />
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最佳选择方式:"
android:textcolor="#005ab5" />
<textview
android:id="@+id/locationproid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textcolor="#8f4586" />
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经度:"
android:textcolor="#005ab5" />
<edittext
android:id="@+id/latedittextid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:textcolor="#8f4586" />
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="纬度:"
android:textcolor="#005ab5" />
<edittext
android:id="@+id/lonedittextid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:textcolor="#8f4586" />
</linearlayout>
</linearlayout>
</linearlayout>
<---activity->
package com.talkweb.mobileapp;
import java.text.decimalformat;
import android.app.activity;
import android.content.context;
import android.location.criteria;
import android.location.location;
import android.location.locationlistener;
import android.location.locationmanager;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.edittext;
import android.widget.imagebutton;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.textview;
import android.widget.radiogroup.oncheckedchangelistener;
/**
*
* @author mr.z
* @time 2012-5-16
*
*/
public class locationappactivity extends activity {
private imagebutton btngetbestlocationpro;
private edittext txtlat;
private edittext txtlon;
private textview txtlocationpro;
private locationmanager locationmanager;
private decimalformat format;
private string provider;
private radiogroup providergroup;
private radiobutton radgps;
private radiobutton radnetwork;
private string latstr;
private string lonstr;
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
this.setcontentview(r.layout.main);
btngetbestlocationpro = (imagebutton) findviewbyid(r.id.bestlocationproid);
btngetbestlocationpro.setonclicklistener(new getbestlocationprolistener());
txtlat = (edittext) findviewbyid(r.id.latedittextid);
txtlon = (edittext) findviewbyid(r.id.lonedittextid);
txtlocationpro = (textview) findviewbyid(r.id.locationproid);
format = new decimalformat("#.000000");
locationmanager = (locationmanager) locationappactivity.this.getsystemservice(context.location_service);
criteria criteria = new criteria();
criteria.setaccuracy(criteria.accuracy_fine);
criteria.setpowerrequirement(criteria.power_low);
criteria.setaltituderequired(false);
criteria.setspeedrequired(false);
criteria.setcostallowed(false);
provider = locationmanager.getbestprovider(criteria, false);
txtlocationpro.settext(provider);
locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, new testlocationlistener());
providergroup = (radiogroup) findviewbyid(r.id.providergroup);
radgps = (radiobutton) findviewbyid(r.id.gpsprovide);
radnetwork = (radiobutton) findviewbyid(r.id.networkprovide);
providergroup.setoncheckedchangelistener(new locationprovidecheckedlistener());
if (provider.equals(locationmanager.gps_provider)) {
system.out.println("gps");
radgps.setselected(true);
radgps.setchecked(true);
} else if (provider.equals(locationmanager.network_provider)) {
system.out.println("network");
radnetwork.setselected(true);
radnetwork.setchecked(true);
}
}
private class getbestlocationprolistener implements onclicklistener {
@override
public void onclick(view v) {
criteria criteria = new criteria();
criteria.setaccuracy(criteria.accuracy_fine);
criteria.setpowerrequirement(criteria.power_low);
criteria.setaltituderequired(false);
criteria.setspeedrequired(false);
criteria.setcostallowed(false);
string provider = locationmanager.getbestprovider(criteria, false);
txtlocationpro.settext(provider);
locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, new testlocationlistener());
}
}
private class testlocationlistener implements locationlistener {
@override
public void onlocationchanged(location location) {
double lat = location.getlatitude();
double lon = location.getlongitude();
latstr = format.format(lat);
lonstr = format.format(lon);
txtlat.settext(latstr);
txtlon.settext(lonstr);
}
@override
public void onproviderdisabled(string provider) {
}
@override
public void onproviderenabled(string provider) {
}
@override
public void onstatuschanged(string provider, int status, bundle extras) {
}
}
private class locationprovidecheckedlistener implements oncheckedchangelistener {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
if (checkedid == radgps.getid()) {
locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, new testlocationlistener());
} else if (checkedid == radnetwork.getid()) {
locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, new testlocationlistener());
}
}
}
}
权限:
<uses-permission android:name="android.permission.camera" />
<uses-permission android:name="android.permission.vibrate" />
<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.access_fine_location" />
<uses-permission android:name="android.permission.access_location_extra_commands" />
<uses-permission android:name="android.permission.read_phone_state" />
<uses-permission android:name="android.permission.internet" />
<uses-permission android:name="android.permission.receive_sms" />
<uses-permission android:name="android.permission.record_audio" />
<uses-permission android:name="android.permission.modify_audio_settings" />
<uses-permission android:name="android.permission.read_contacts" />
<uses-permission android:name="android.permission.write_contacts" />
<uses-permission android:name="android.permission.write_external_storage" />
<uses-permission android:name="android.permission.access_network_state" />
<uses-permission android:name="android.permission.change_configuration" />
<uses-permission android:name="android.permission.wake_lock" />
<uses-permission android:name="android.permission.bluetooth" />
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffd3d7df"
android:orientation="vertical" >
<linearlayout
android:id="@+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginbottom="20dip"
android:layout_marginleft="20dip"
android:layout_marginright="20dip"
android:layout_margintop="20dip"
android:background="@drawable/bg_frame"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingbottom="2dip"
android:paddingleft="10dip"
android:paddingright="10dip"
android:paddingtop="10dip" >
<textview
android:id="@+id/providertitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取经纬度:"
android:textcolor="#007979" />
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<radiogroup
android:id="@+id/providergroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<radiobutton
android:id="@+id/gpsprovide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="gps"
android:textcolor="#005ab5" />
<radiobutton
android:id="@+id/networkprovide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="基站"
android:textcolor="#005ab5" />
</radiogroup>
<imagebutton
android:id="@+id/bestlocationproid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/loction" />
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最佳选择方式:"
android:textcolor="#005ab5" />
<textview
android:id="@+id/locationproid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textcolor="#8f4586" />
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经度:"
android:textcolor="#005ab5" />
<edittext
android:id="@+id/latedittextid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:textcolor="#8f4586" />
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="纬度:"
android:textcolor="#005ab5" />
<edittext
android:id="@+id/lonedittextid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:textcolor="#8f4586" />
</linearlayout>
</linearlayout>
</linearlayout>
<---activity->
复制代码 代码如下:
package com.talkweb.mobileapp;
import java.text.decimalformat;
import android.app.activity;
import android.content.context;
import android.location.criteria;
import android.location.location;
import android.location.locationlistener;
import android.location.locationmanager;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.edittext;
import android.widget.imagebutton;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.textview;
import android.widget.radiogroup.oncheckedchangelistener;
/**
*
* @author mr.z
* @time 2012-5-16
*
*/
public class locationappactivity extends activity {
private imagebutton btngetbestlocationpro;
private edittext txtlat;
private edittext txtlon;
private textview txtlocationpro;
private locationmanager locationmanager;
private decimalformat format;
private string provider;
private radiogroup providergroup;
private radiobutton radgps;
private radiobutton radnetwork;
private string latstr;
private string lonstr;
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
this.setcontentview(r.layout.main);
btngetbestlocationpro = (imagebutton) findviewbyid(r.id.bestlocationproid);
btngetbestlocationpro.setonclicklistener(new getbestlocationprolistener());
txtlat = (edittext) findviewbyid(r.id.latedittextid);
txtlon = (edittext) findviewbyid(r.id.lonedittextid);
txtlocationpro = (textview) findviewbyid(r.id.locationproid);
format = new decimalformat("#.000000");
locationmanager = (locationmanager) locationappactivity.this.getsystemservice(context.location_service);
criteria criteria = new criteria();
criteria.setaccuracy(criteria.accuracy_fine);
criteria.setpowerrequirement(criteria.power_low);
criteria.setaltituderequired(false);
criteria.setspeedrequired(false);
criteria.setcostallowed(false);
provider = locationmanager.getbestprovider(criteria, false);
txtlocationpro.settext(provider);
locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, new testlocationlistener());
providergroup = (radiogroup) findviewbyid(r.id.providergroup);
radgps = (radiobutton) findviewbyid(r.id.gpsprovide);
radnetwork = (radiobutton) findviewbyid(r.id.networkprovide);
providergroup.setoncheckedchangelistener(new locationprovidecheckedlistener());
if (provider.equals(locationmanager.gps_provider)) {
system.out.println("gps");
radgps.setselected(true);
radgps.setchecked(true);
} else if (provider.equals(locationmanager.network_provider)) {
system.out.println("network");
radnetwork.setselected(true);
radnetwork.setchecked(true);
}
}
private class getbestlocationprolistener implements onclicklistener {
@override
public void onclick(view v) {
criteria criteria = new criteria();
criteria.setaccuracy(criteria.accuracy_fine);
criteria.setpowerrequirement(criteria.power_low);
criteria.setaltituderequired(false);
criteria.setspeedrequired(false);
criteria.setcostallowed(false);
string provider = locationmanager.getbestprovider(criteria, false);
txtlocationpro.settext(provider);
locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, new testlocationlistener());
}
}
private class testlocationlistener implements locationlistener {
@override
public void onlocationchanged(location location) {
double lat = location.getlatitude();
double lon = location.getlongitude();
latstr = format.format(lat);
lonstr = format.format(lon);
txtlat.settext(latstr);
txtlon.settext(lonstr);
}
@override
public void onproviderdisabled(string provider) {
}
@override
public void onproviderenabled(string provider) {
}
@override
public void onstatuschanged(string provider, int status, bundle extras) {
}
}
private class locationprovidecheckedlistener implements oncheckedchangelistener {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
if (checkedid == radgps.getid()) {
locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, new testlocationlistener());
} else if (checkedid == radnetwork.getid()) {
locationmanager.requestlocationupdates(locationmanager.network_provider, 0, 0, new testlocationlistener());
}
}
}
}
权限:
复制代码 代码如下:
<uses-permission android:name="android.permission.camera" />
<uses-permission android:name="android.permission.vibrate" />
<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.access_fine_location" />
<uses-permission android:name="android.permission.access_location_extra_commands" />
<uses-permission android:name="android.permission.read_phone_state" />
<uses-permission android:name="android.permission.internet" />
<uses-permission android:name="android.permission.receive_sms" />
<uses-permission android:name="android.permission.record_audio" />
<uses-permission android:name="android.permission.modify_audio_settings" />
<uses-permission android:name="android.permission.read_contacts" />
<uses-permission android:name="android.permission.write_contacts" />
<uses-permission android:name="android.permission.write_external_storage" />
<uses-permission android:name="android.permission.access_network_state" />
<uses-permission android:name="android.permission.change_configuration" />
<uses-permission android:name="android.permission.wake_lock" />
<uses-permission android:name="android.permission.bluetooth" />