gpt4 book ai didi

安卓谷歌地图。按地址添加标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:18 24 4
gpt4 key购买 nike

我在 android 应用程序中添加了 map ,并希望通过地址在 map 上添加标记。有可能的?我已经尝试使用 Geocoder 获取 long 和 lat,但出现错误 Service not Available

我的代码:

Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;

try {
addresses = geocoder.getFromLocationName(event.getPlace(), 20);
System.out.println(addresses);
// for (int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES
//
// Address addr = addresses.get(i);
//
// double latitude = addr.getLatitude();
// double longitude = addr.getLongitude(); // DO SOMETHING WITH
// // VALUES
//
// System.out.println(latitude);
// System.out.println(longitude);
//
// }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

创建一个将地址转换为 LatLng 的方法:

public LatLng getLocationFromAddress(Context context, String strAddress)
{
Geocoder coder= new Geocoder(context);
List<Address> address;
LatLng p1 = null;

try
{
address = coder.getFromLocationName(strAddress, 5);
if(address==null)
{
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();

p1 = new LatLng(location.getLatitude(), location.getLongitude());
}
catch (Exception e)
{
e.printStackTrace();
}
return p1;

}

然后,

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng address = getLocationFromAddress(this, yourAddressString(eg. "Street Number, Street, Suburb, State, Postcode");
mMap.addMarker(new MarkerOptions().position(address).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(address));
}

关于安卓谷歌地图。按地址添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24352192/

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