gpt4 book ai didi

android - Android 的前向地理编码示例代码?

转载 作者:搜寻专家 更新时间:2023-11-01 08:15:58 25 4
gpt4 key购买 nike

有没有人开发过将地址转换为纬度/经度值的代码。我对地理编码器感到困惑。如果我能得到示例代码,那将很有帮助,我正在使用下面的代码。它总是给出错误 no address found。不会崩溃

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myMap = (MapView) findViewById(R.id.simpleGM_map); // Get map from XML
btnSearch = (Button) findViewById(R.id.simpleGM_btn_search); // Get button from xml
adress = (EditText) findViewById(R.id.simpleGM_adress); // Get address from XML

gc = new Geocoder(this); // create new geocoder instance

btnSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

pd = ProgressDialog.show(simpleGoogleMaps.this, "Working..", "Searching your address", true, false); //Show a progress dialog
//Thread thread = new Thread(simpleGoogleMaps.this); //then create a new thread
// thread.start(); //and start the thread. it will automatically call the standard run-function.

searchAdress = new Thread() {
public void run(){
String addressInput = adress.getText().toString(); // Get input text
try {
foundAdresses = gc.getFromLocationName(addressInput, 5); // Search addresses
Thread.sleep(1500);
} catch (Exception e) {
// @todo: Show error message
}
showAdressResults.sendEmptyMessage(0);
}
};
searchAdress.start();

}
});
}

private Handler showAdressResults = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();

if (foundAdresses.size() == 0) { // if no address found,
// display an error
Dialog locationError = new AlertDialog.Builder(
simpleGoogleMaps.this).setIcon(0).setTitle(
"Error").setPositiveButton(R.string.ok, null)
.setMessage(
"Sorry, your address doesn't exist.")
.create();
locationError.show();

} else { // else display address on map
for (int i = 0; i < foundAdresses.size(); ++i) {
// Save results as Longitude and Latitude
// @todo: if more than one result, then show a
// select-list
Address x = foundAdresses.get(i);
lat = x.getLatitude();
lon = x.getLongitude();
}
navigateToLocation((lat * 1000000), (lon * 1000000),myMap); // display the found address
}

}
};

/**
* Navigates a given MapView to the specified Longitude and Latitude
*
* @param latitude
* @param longitude
* @param mv
*/
public static void navigateToLocation(double latitude, double longitude, MapView mv) {
GeoPoint p = new GeoPoint((int) latitude, (int) longitude); // new
// GeoPoint
mv.displayZoomControls(true); // display Zoom (seems that it doesn't
// work yet)
MapController mc = mv.getController();
mc.animateTo(p); // move map to the given point
int zoomlevel = mv.getMaxZoomLevel(); // detect maximum zoom level
mc.setZoom(zoomlevel - 1); // zoom
mv.setSatellite(false); // display only "normal" mapview
}
}

最佳答案

下面的链接可能对你有用 Get the lat and lon according to address

关于android - Android 的前向地理编码示例代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4942213/

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