gpt4 book ai didi

java - 如何在android中获取位置?

转载 作者:行者123 更新时间:2023-12-01 13:13:02 24 4
gpt4 key购买 nike

我已经有了获取地址的代码。但我不知道是否得到了真正的纬度和经度。如何更改我的代码?这样我就可以使用真正的代码了。谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢

package ie.wit.mybook;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.gms.maps.GoogleMap;
import ie.wit.mybook.*;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AdressGet extends Activity {

double LATITUDE=52.7;
double LONGITUDE=-7.12;
GPSHelper mylo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adressget);
TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
TextView myAddress = (TextView)findViewById(R.id.myaddress);

myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));

Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}

}
}

最佳答案

如果需要获取真实坐标,则需要使用LocationListener。试试这个:http://www.vogella.com/tutorials/AndroidLocationAPI/article.html

关于java - 如何在android中获取位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com