search
HomeJavajavaTutorialAndroid mobile phone obtains the longitude and latitude address of GPS and base station implementation code

<?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>

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()); 
} 
} 
} 
}

Permissions:

<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" />


More Android mobile phone obtains the latitude and longitude address of GPS and base station. For code-related articles, please pay attention to the PHP Chinese website!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What aspects of Java development are platform-dependent?What aspects of Java development are platform-dependent?Apr 26, 2025 am 12:19 AM

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Are there performance differences when running Java code on different platforms? Why?Are there performance differences when running Java code on different platforms? Why?Apr 26, 2025 am 12:15 AM

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

What are some limitations of Java's platform independence?What are some limitations of Java's platform independence?Apr 26, 2025 am 12:10 AM

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Explain the difference between platform independence and cross-platform development.Explain the difference between platform independence and cross-platform development.Apr 26, 2025 am 12:08 AM

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?Apr 26, 2025 am 12:02 AM

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Why is Java a popular choice for developing cross-platform desktop applications?Why is Java a popular choice for developing cross-platform desktop applications?Apr 25, 2025 am 12:23 AM

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Discuss situations where writing platform-specific code in Java might be necessary.Discuss situations where writing platform-specific code in Java might be necessary.Apr 25, 2025 am 12:22 AM

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

What are the future trends in Java development that relate to platform independence?What are the future trends in Java development that relate to platform independence?Apr 25, 2025 am 12:12 AM

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages ​​such as Python and JavaScript to enhance cross-language interoperability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools