android手機擷取gps和基站的經緯度地址實現代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:<?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" />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.