gpt4 book ai didi

android - 如何从android中的 map 获取地址?

转载 作者:行者123 更新时间:2023-11-29 20:58:33 24 4
gpt4 key购买 nike

我正在构建一个 android 应用程序,用户可以从 map 中选择地址作为他最喜欢的地方。

我需要的是,当任何用户双击任何地方时,它应该显示在我的 TextView 中,并且它应该以 100% 缩放工作。

这是我的代码 -

    package com.amal.googlemap;


public class MainActivitytut extends FragmentActivity{

GoogleMap map;
private static final LatLng GOLDEN_GATE_BRIDGE =
new LatLng(37.828891,-122.485884);
private static final LatLng APPLE =
new LatLng(37.3325004578, -122.03099823);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintut);

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (map == null) {
Toast.makeText(this, "Google Maps not available",
Toast.LENGTH_LONG).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
// present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.menu_sethybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;

case R.id.menu_showtraffic:
map.setTrafficEnabled(true);
break;

case R.id.menu_zoomin:
map.animateCamera(CameraUpdateFactory.zoomIn());
break;

case R.id.menu_zoomout:
map.animateCamera(CameraUpdateFactory.zoomOut());
break;

case R.id.menu_gotolocation:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(GOLDEN_GATE_BRIDGE) // Sets the center of the map to
// Golden Gate Bridge
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(
cameraPosition));
break;

case R.id.menu_addmarker:
// ---using the default marker---
/*
map.addMarker(new MarkerOptions()
.position(GOLDEN_GATE_BRIDGE)
.title("Golden Gate Bridge") .snippet("San Francisco")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
*/

map.addMarker(new MarkerOptions()
.position(GOLDEN_GATE_BRIDGE)
.title("Golden Gate Bridge")
.snippet("San Francisco")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
break;

case R.id.menu_getcurrentlocation:
// ---get your current location and display a blue dot---
map.setMyLocationEnabled(true);

break;

case R.id.menu_showcurrentlocation:
Location myLocation = map.getMyLocation();
LatLng myLatLng = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());

CameraPosition myPosition = new CameraPosition.Builder()
.target(myLatLng).zoom(17).bearing(90).tilt(30).build();
map.animateCamera(
CameraUpdateFactory.newCameraPosition(myPosition));
break;

case R.id.menu_lineconnecttwopoints:
//---add a marker at Apple---
map.addMarker(new MarkerOptions()
.position(APPLE)
.title("Apple")
.snippet("Cupertino")
.icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_AZURE)));

//---draw a line connecting Apple and Golden Gate Bridge---
map.addPolyline(new PolylineOptions()
.add(GOLDEN_GATE_BRIDGE, APPLE).width(5).color(Color.RED));
break;
}
return true;
}
}

我尝试了很多方法,但没有一个能达到我需要的效果....请帮助..

最佳答案

试试这个可能对你有帮助,

Address address = getAddressFromLatLong(context,lat,long);
public static Address getAddressFromLatLong(Context context,double lat, double lng) {

geocoder = new Geocoder(context);

List<Address> list = null;
try {
list = geocoder.getFromLocation(lat, lng, 10);
return list.get(0);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}

关于android - 如何从android中的 map 获取地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26879499/

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