Android 我试图得到我的当前位置,但这显示错误。我还尝试使用以下代码获取我的位置,但它不起作用
我正在使用此代码,但它不起作用Android 我试图得到我的当前位置,但这显示错误。我还尝试使用以下代码获取我的位置,但它不起作用,android,location,Android,Location,我正在使用此代码,但它不起作用 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { if (ActivityCompat.checkSelfPermiss
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//get latitude
double latitude = location.getLatitude();
//get Longtitude
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
// Toast.makeText(getApplicationContext(), latitude+" "+longitude,
Toast.LENGTH_LONG).show();
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
String str = addresses.get(0).getLocality() + ",";
str += addresses.get(0).getCountryName();
mMap.addMarker(new MarkerOptions().position(latLng).title(str));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//get latitude
double latitude = location.getLatitude();
//get Longtitude
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
String str = addresses.get(0).getLocality()+",";
str +=addresses.get(0).getCountryName();
mMap.addMarker(new MarkerOptions().position(latLng).title(str));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
如何解决此问题?您在清单中有权限吗
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
以及我测试的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/crna">
<Button
android:id="@+id/btnShowLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Location" />
<TextView
android:id="@+id/tvAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnShowLocation"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="124dp"
android:layout_marginBottom="134dp" />
</RelativeLayout>
看来编辑打断了你的问题。他们干掉了安卓。它有很大的不同。这是一个示例程序吗?是的,它是一个简单的程序
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Tab5Fragment extends Fragment {
TextView tvAddress;
Button btnShowLocation;
GPSTracker gps;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab5, container, false);
tvAddress = view.findViewById(R.id.tvAddress);
btnShowLocation = view.findViewById(R.id.btnShowLocation);
// Show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create class object
gps = new GPSTracker(getActivity());
// Check if GPS enabled
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
GPSTracker.LocationAddress locationAddress = new GPSTracker.LocationAddress();
locationAddress.getAddressFromLocation(latitude, longitude,
getContext(), new GeocoderHandler());
// \n is for new line
Toast.makeText(getActivity(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
String result = "Latitude: " + latitude + " Longitude: " + longitude;
tvAddress.setText(result);
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
// Setting DialogHelp Title
alertDialog.setTitle("GPS is settings");
// Setting DialogHelp Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
getActivity().startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
}
});
return view;
}
class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
String locationAddress;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
break;
default:
locationAddress = null;
}
tvAddress.setText(locationAddress);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/crna">
<Button
android:id="@+id/btnShowLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Location" />
<TextView
android:id="@+id/tvAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnShowLocation"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="124dp"
android:layout_marginBottom="134dp" />
</RelativeLayout>