android 基站经纬度,android手机获取gps和基站的经纬度地址实现代码

发布时间:2025-12-09 20:16:24 浏览次数:4

代码如下:

代码如下:

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

}

}

}

}

权限:

代码如下:

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477