SoFunction
Updated on 2025-03-11

Android mobile phone to obtain the latitude and longitude address implementation code of GPS and base station

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFD3D7DF"
android:orientation="vertical" >
<LinearLayout
android:
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:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="get latitude and longitude:"
android:textColor="#007979" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="gps"
android:textColor="#005AB5" />
<RadioButton
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="base station"
android:textColor="#005AB5" />
</RadioGroup>
<ImageButton
android:
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="Best way to choose:"
android:textColor="#005AB5" />
<TextView
android:
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="Longitude:"
android:textColor="#005AB5" />
<EditText
android:
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="latitude:"
android:textColor="#005AB5" />
<EditText
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:textColor="#8F4586" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

<---activity->
Copy the codeThe code is as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
*
* @author
* @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) {
(savedInstanceState);
();
btnGetBestLocationPro = (ImageButton) findViewById();
(new GetBestLocationProListener());
txtLat = (EditText) findViewById();
txtLon = (EditText) findViewById();
txtLocationPro = (TextView) findViewById();
format = new DecimalFormat("#.000000");
locationManager = (LocationManager) (Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
(Criteria.ACCURACY_FINE);
(Criteria.POWER_LOW);
(false);
(false);
(false);
provider = (criteria, false);
(provider);
(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
providerGroup = (RadioGroup) findViewById();
radGps = (RadioButton) findViewById();
radNetwork = (RadioButton) findViewById();
(new LocationProvideCheckedlistener());
if ((LocationManager.GPS_PROVIDER)) {
("gps");
(true);
(true);
} else if ((LocationManager.NETWORK_PROVIDER)) {
("network");
(true);
(true);
}
}
private class GetBestLocationProListener implements OnClickListener {
@Override
public void onClick(View v) {
Criteria criteria = new Criteria();
(Criteria.ACCURACY_FINE);
(Criteria.POWER_LOW);
(false);
(false);
(false);
String provider = (criteria, false);
(provider);
(LocationManager.NETWORK_PROVIDER, 0, 0, new TestLocationListener());
}
}
private class TestLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
double lat = ();
double lon = ();
latStr = (lat);
lonStr = (lon);
(latStr);
(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 == ()) {
(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
} else if (checkedId == ()) {
(LocationManager.NETWORK_PROVIDER, 0, 0, new TestLocationListener());
}
}
}
}

Permissions:
Copy the codeThe code is as follows:

<uses-permission android:name="" />
<uses-permission android:name="" />
<uses-permission android:name=".ACCESS_COARSE_LOCATION" />
<uses-permission android:name=".ACCESS_FINE_LOCATION" />
<uses-permission android:name=".ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name=".READ_PHONE_STATE" />
<uses-permission android:name="" />
<uses-permission android:name=".RECEIVE_SMS" />
<uses-permission android:name=".RECORD_AUDIO" />
<uses-permission android:name=".MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name=".READ_CONTACTS" />
<uses-permission android:name=".WRITE_CONTACTS" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".CHANGE_CONFIGURATION" />
<uses-permission android:name=".WAKE_LOCK" />
<uses-permission android:name="" />