AndroidはIPアドレスから都市を取得します。
4117 ワード
Androidは携帯電話の現在の経緯度を取得します。参考してください。http://blog.csdn.net/a511341250/article/details/40509931
manifestに加えたいpermissionとactivitymain.xmlを参照してください。
MainActivity.java
使っているjarカバンはAndroid_です。Location_V 1.1.1.1.jar
manifestに加えたいpermissionとactivitymain.xmlを参照してください。
MainActivity.java
package com.example.test;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.widget.EditText;
public class MainActivity extends Activity {
LocationManager lm;
EditText et;
LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location){
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Location l = lm.getLastKnownLocation(provider);
updateView(l);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.et);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String bestProvider = lm.getBestProvider(getCriteria(), true);
Location l = lm.getLastKnownLocation(bestProvider);
updateView(l);
lm.requestLocationUpdates(bestProvider, 5000, 8, ll);
}
private Criteria getCriteria() {
// TODO Auto-generated method stub
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_COARSE);
c.setSpeedRequired(false);
c.setCostAllowed(false);
c.setBearingRequired(false);
c.setAltitudeRequired(false);
c.setPowerRequirement(Criteria.POWER_LOW);
return c;
}
public void updateView(Location newLocation)
{
if(newLocation !=null){
String latitude = String.valueOf(newLocation.getLatitude());
String longitude = String.valueOf(newLocation.getLongitude());
double Latitude = newLocation.getLatitude();
double Longitude = newLocation.getLongitude();
String city = getAddressbyGeoPoint(Latitude,Longitude);
et.setText("
:");
et.append(latitude);
et.append("
:");
et.append(longitude);
et.append("
:");
et.append(city);
}
else{
et.getEditableText().clear();
}
}
// Geopoint Address
public String getAddressbyGeoPoint(double Latitude, double Longitude)
{
String strReturn = "";
try
{
/* GeoPoint null */
// if (gp != null)
// {
/* Geocoder , */
Geocoder gc = new Geocoder(MainActivity.this, Locale.getDefault());
/* */
double geoLatitude = Latitude;
double geoLongitude = Longitude;
/* ( )*/
List lstAddress = gc.getFromLocation(geoLatitude, geoLongitude, 1);
StringBuilder sb = new StringBuilder();
/* */
if (lstAddress.size() > 0)
{
Address adsLocation = lstAddress.get(0);
for (int i = 0; i < adsLocation.getMaxAddressLineIndex(); i++)
{
sb.append(adsLocation.getAddressLine(i)).append("
"); //
}
sb.append(adsLocation.getLocality()).append("
"); // ( )
// sb.append(adsLocation.getPostalCode()).append("
");
// sb.append(adsLocation.getCountryName());
}
/* stringbuilder */
strReturn = sb.toString();
// }
}
catch(Exception e)
{
e.printStackTrace();
}
return strReturn;
}
}
ka 2008の返信を参照してください。http://www.eoeandroid.com/forum.php?mod=viewthread&tid=23068&page=1#pid216151 使っているjarカバンはAndroid_です。Location_V 1.1.1.1.jar