我正在尝试从Android Google Maps v2中获取经纬度的地址,但它有误。
这是我的代码:
function type() {
var el = document.getElementById('test').querySelector(' p');
var slogan = el.innerHTML;
el.innerHTML = '';
var i = 0;
setInterval(function() {
var char = slogan.substr(i, 1);
el.innerHTML += char;
i++;
}, 250); // change 250 as per typing speed you need
}
type(); // call the function whenever you need
答案 0 :(得分:7)
使用以下代码段:
try {
Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
yourtextfieldname.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
参考: https://mobiforge.com/design-development/using-google-maps-android
答案 1 :(得分:1)
这应该为您提供lat long的地址
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(mContext, Locale.getDefault());
try {
String sPlace;
addresses = geocoder.getFromLocation(mLat, mLong, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
String[] splitAddress = address.split(",");
sPlace = splitAddress[0] + "\n";
if(city != null && !city.isEmpty()) {
String[] splitCity = city.split(",");
sPlace += splitCity[0];
}
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
我正在给你我的代码,我使用这个代码,2个月前..我现在无法测试这段代码,但它确实有效。你可能需要修改一些行。 使用此功能
public void getAddressByLatLong(){
latitude = bundle.getDouble("Lat");
longitude = bundle.getDouble("Long");
LatLng pos = new LatLng(latitude, longitude);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(googleMap.MAP_TYPE_HYBRID);
LocationManager location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener listner = new MyLocationListner();
location_manager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 3000, 5000, listner);
} catch (Exception e) {
e.printStackTrace();
}
}
创建一个类
public class MyLocationListner implements LocationListener {
@SuppressLint("NewApi")
@SuppressWarnings("static-access")
@Override
public void onLocationChanged(Location arg0) {
Toast.makeText(getApplicationContext(),
"In on Location Changed", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
getLatitude = "" + arg0.getLatitude();
getLongitude = "" + arg0.getLongitude();
// lati.setText(getLatitude + "," + getLongitude);
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
try {
geocoder = new Geocoder(DisplayActivity.this, Locale.ENGLISH);
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (geocoder.isPresent()) {
Toast.makeText(getApplicationContext(), "geocoder present",
Toast.LENGTH_SHORT).show();
Address returnAddress = addresses.get(0);
String localityString = returnAddress.getLocality();
String city = returnAddress.getCountryName();
String region_code = returnAddress.getCountryCode();
String zipcode = returnAddress.getPostalCode();
str = new StringBuilder();
str.append(localityString + " ");
str.append(city + " ");
str.append(region_code + " ");
str.append(zipcode + " ");
// Add = str.toString();
// longi.setText(str);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(2)
.build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title(
str.toString());
marker.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mario));
googleMap.addMarker(marker);
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(),
"geocoder not present", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Exception",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}