2018年11月30日 星期五

[Android]_ch9.7_地名或地址轉成位置





[Android]_ch9.7_地名或地址轉成位置



呼叫Geocoder.getFromLocationName(String
locationName, int maxResults)可將使用者輸入的地名/地址(LocationName)轉成List<Address>
Address物件可以呼叫getLatitude()getLongitude()得到緯經度


參數maxResults用來限定回傳的資料筆數


et1=(EditText)findViewById(R.id.editText1);

        btn1=(Button)findViewById(R.id.button1);

        btn1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String name=et1.getText().toString();

Geocoder gc=new Geocoder(MainActivity.this);

List<Address> list=null;

try {

list=gc.getFromLocationName(name, 3);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(list.isEmpty() || list==null)

Toast.makeText(MainActivity.this, "無法找到該位置!", Toast.LENGTH_SHORT).show();

else{

Address address=list.get(0);

double lat=address.getLatitude();

double lng=address.getLongitude();

MarkerOptions mo=new MarkerOptions();

mo.position(new LatLng(lat, lng));

mo.title(name);

mo.snippet(address.getAddressLine(0));

mo.icon(BitmapDescriptorFactory.fromResource(R.drawable.m1));

map.addMarker(mo);

CameraPosition cp=new CameraPosition.Builder().target(new LatLng(lat, lng)).zoom(15).build();

CameraUpdate cu=CameraUpdateFactory.newCameraPosition(cp);

map.animateCamera(cu);

}

}

});

沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...