java 在android studio中使用标记获取当前位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32840371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:51:54 来源:igfitidea点击:
Get current location with marker in android studio
提问by Meli
How to find current location with a marker. This code it finds the location but how to show the marker in current location?
如何使用标记查找当前位置。这段代码可以找到位置,但如何在当前位置显示标记?
public void onSearch (View view)
{
ibtn = (ImageButton)findViewById(R.id.imageSearchButton);
EditText location_tf = (EditText) findViewById(R.id.editTextSearch);
String location = location_tf.getText().toString();
List<Address> addressList = null;
if(location != null || !location.equals(""))
{
Geocoder geocoder = new Geocoder(this);
try{
addressList = geocoder.getFromLocationName(location, 1);
}catch (IOException e)
{
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Accidents Here"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
采纳答案by Ganpat Kaliya
try this,
试试这个,
write onCreate() in your main activity class.
在您的主要活动类中编写 onCreate() 。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gooogle_map);
TextView tvAddress = (TextView) findViewById(R.id.textView1);
GoogleMap googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i = 0; i < providers.size(); i++) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) {
latitude = l.getLatitude();
longitude = l.getLongitude();
strAdd = getCompleteAddressString(latitude, longitude);
tvAddress.setText("Complete Address : " + strAdd);
break;
}
}
if (googleMap != null) {
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps").snippet("Discription");
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
// Moving Camera to a Location with animation
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
googleMap.addMarker(marker);
}
}
and this is your getCompleteAddressString().
这是您的 getCompleteAddressString()。
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<android.location.Address> addresses = geocoder
.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
android.location.Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address",
"" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
return strAdd;
}