我是android的新手,我被指派对以前在 eclipse 中制作的项目进行一些更改,现在我们正在使用 android studio 。
我在互联网上搜索了很多,但我没有得到解决方案,我必须在代码中进行更改。
我的代码
`package com.attorneygeneral;`
public class GetLocation extends Activity实现LocationListener {
ImageView img_back,img_search;
EditText search_loc;
TextView txt_location,txt_longitude,txt_latitude;
private GoogleMap googleMap;
double lat,lng;
static LatLng currentLoc;
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_location);
img_back=(ImageView) findViewById(R.id.img_back);
img_search=(ImageView) findViewById(R.id.img_search);
search_loc=(EditText) findViewById(R.id.search_loc);
txt_location=(TextView) findViewById(R.id.txt_location);
txt_latitude=(TextView) findViewById(R.id.latitude);
txt_longitude=(TextView) findViewById(R.id.longitude);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 900000, 100,
GetLocation.this);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager
.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider
+ " has been selected.");
onLocationChanged(location);
} else {
Toast.makeText(getApplicationContext(),
"Location not available", Toast.LENGTH_LONG).show();
}
try
{
if(googleMap==null)
{
googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
}
/*Marker hardingLaw = googleMap.addMarker(new MarkerOptions().
position(currentLoc).title("Location"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);*/
}
catch (Exception e){
e.printStackTrace();
}
search_loc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
search_loc.setFocusable(true);
}
});
img_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
img_search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!TextUtils.isEmpty(search_loc.getText().toString())) {
}
}
});
}
@Override
public void onLocationChanged(Location location) {
// txtLat = (TextView) findViewById(R.id.textview1);
// txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"
// + location.getLongitude());
System.out.println("Latitude Location :" + location.getLongitude()
+ "::" + location.getLatitude());
lat = location.getLatitude();
lng = location.getLongitude();
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
for (int i = 0; i < maxLines; i++) {
String addressStr = address.get(0).getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
String finalAddress = builder.toString(); // This is the complete
// address.
System.out.println("address :" + finalAddress);
txt_latitude.setText(String.valueOf(lat));
txt_longitude.setText(String.valueOf(lng));
txt_location.setText(finalAddress); // This will display the final address.
currentLoc = new LatLng(lat, lng);
Marker hardingLaw = googleMap.addMarker(new MarkerOptions().
position(currentLoc).title("Location"));
//hardingLaw.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_pin));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 15));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
catch (IOException e)
{
}
catch (NullPointerException e)
{
}
}
@Override
public void onProviderDisabled(String provider) {
System.out.println("Latitude disable");
}
@Override
public void onProviderEnabled(String provider) {
System.out.println("Latitude enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("Latitude status");
}
}`