Android Geocoder

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

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的模拟器上做测试,不然不能成功的!!
相关资讯
全链就绪!IICIE国际集成电路创新博览会9月9日深圳启幕

2026 IICIE国际集成电路创新博览会(IC创新博览会)将于9月9-11日在深圳国际会展中心(宝安)盛大举办。本届展会以“跨界融合·全链协同,共筑特色芯生态”为主题,在深圳国际会展中心14-16号馆集中展示

新品!GMSL高性能视觉,驱动机器人创新应用-MRC-P5760

我爱方案网推荐MRC-P5760机器人控制器,专为具身智能、AGV、AMR、服务机器人等场景打造。8路GMSL摄像头直连 + 4路千兆网口 + 2路CAN FD,高带宽低延迟,适配高阶机器人视觉方案

行业爆发,AI升级!13万+观众、意向成交39亿元,IOTE 2026深圳物联网展圆满收官

本届展会汇聚全行业顶尖智慧,围绕无源物联生态、高精度定位、边缘计算、通用AI、智能硬件创新等核心议题深度研讨,打破行业信息壁垒,凝聚产业发展共识,为行业技术创新、模式升级、生态共建输出了极具价值的新思路、新方案、新方向。

突发!PCB巨头遭大搜查,14 人被控制!

全球PCB与IC载板巨头欣兴电子近日遭桃园地方检察署大规模搜查

贸泽新一期EIT系列探索量子计算的未来及其促进工程领域转型的潜力

本期将探讨量子计算这一新兴领域,并研究如何利用量子力学原理解锁新的计算能力,以解决传统计算架构仍然难以解决的问题。