Android Geocoder

发布时间:2010-10-23 阅读量:1900 来源: 发布人:

package com.hl;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.MapActivity;
public class Geo extends MapActivity {
private EditText input;
private Button btn;
private TextView show;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
input = (EditText)findViewById(R.id.input);
btn = (Button)findViewById(R.id.btn);
show = (TextView)findViewById(R.id.show);

btn.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
show.setText(performGeocode(input.getText().toString(), true));
}
});
}

private String performGeocode(final String in, final boolean isAddr) {
String result= null;
if (this.input != null) {
Geocoder geocoder = new Geocoder(this,Locale.CHINA);
if (isAddr) {
try {
List
addresses = geocoder.getFromLocationName(in,5);
if (addresses != null) {
result = addresses.get(0).toString();
}
} catch (IOException e) {
Toast.makeText(Geo.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
try {
String[] coords = in.split(",");
if ((coords != null) && (coords.length == 2)) {
List
addresses = geocoder.getFromLocation(
Double.parseDouble(coords[0]),
Double.parseDouble(coords[1]), 1);
result = addresses.get(0).toString();
}
} catch (IOException e) {
Toast.makeText(Geo.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
return result;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
注意不要在android 8的模拟器上做测试,不然不能成功的!!
相关资讯
速看!西部电博会开展倒计时,宝藏亮点抢先看

成渝地区双城经济圈已崛起为全国乃至全球极具竞争力的电子信息产业增长极。区域内产业规模高达2.36万亿元,成功跻身国家级战略性产业集群,并构建起覆盖芯片、面板、终端及配套的全链条产业闭环,正加速从传统制造基地向全球前沿创新高地实现历史性跨越。

DigiKey 将在 2026 慕尼黑上海电子展上呈现精彩创新演示、现场访谈和赠品抽奖活动

现场演示、供应商见解以及 DigiKey Moment 得捷时刻活动会通过 B 站频道将展会体验同步延伸到展位之外

DigiKey 推出《可持续的未来》视频系列第二季

新一季视频重点展示了先进电子技术如何助力各行业实现可持续设计。

一套完整的NPU机器人视觉伺服系统——瑞芯微RK3576

随着嵌入式 AI 技术的快速发展,边缘计算设备的算力不断提升,使得在低功耗、低成本的嵌入式平台上部署深度学习模型成为可能

人形机器人关节模组时钟方案—推荐YSO150HT宽温有源晶振

人形机器人必须配套专用晶振才能迈过耐高温、低温漂、抗震动与极致小型化的严苛门槛。