我有一个应用程序,我将在点击时检索用户的位置,但是当用户首次安装该应用程序并单击以获取位置时,在获得在线权限后,地图活动将关闭,并且第一个活动刚出现,此后,该应用程序和位置活动正常进行,没有问题。
public class Maps2 extends AppCompatActivity {
double lat = 0;
double lng = 0;
LatLng myLocation;
GoogleMap map;
Timer timer;
LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
Toast.makeText(getApplicationContext(),"در حال موقعیت یابی لطفا صبور باشید ...", Toast.LENGTH_LONG).show();
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
LatLng point = new LatLng(33.636333, 46.424894);
// map.clear();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 14.0f));
findlocation();
map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
//get latlng at the center by calling
LatLng midLatLng = map.getCameraPosition().target;
lat = midLatLng.latitude;
lng = midLatLng.longitude;
}
});
}// end of on map ready
}); // end of support fragment
FloatingActionButton btn_mylocation = (FloatingActionButton) findViewById(R.id.floatingActionButton_mylocation);
btn_mylocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findlocation();
}
});
FloatingActionButton btn_ok = (FloatingActionButton) findViewById(R.id.floatingActionButton_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (lat != 0 && lng != 0) {
ArrayList<HashMap<String, Object>> BuyList = new ArrayList<HashMap<String, Object>>();
ArrayList<HashMap<String, Object>> all_data = new ArrayList<HashMap<String, Object>>();
String pay, sum, user_mobile, send_price, final_off;
BuyList = (ArrayList<HashMap<String, Object>>) getIntent().getSerializableExtra("data");
all_data = (ArrayList<HashMap<String, Object>>) getIntent().getSerializableExtra("all_data");
user_mobile = getIntent().getStringExtra("mobile");
sum = getIntent().getStringExtra("final_price");
send_price = getIntent().getStringExtra("send_price");
final_off = getIntent().getStringExtra("final_off");
pay = getIntent().getStringExtra("pay");
String add = getIntent().getStringExtra("add");
Intent i = new Intent(Maps2.this, Pay_act.class);
i.putExtra("gps_location", lat + "," + lng);
i.putExtra("data", BuyList);
i.putExtra("pay", pay);
i.putExtra("sum", sum);
i.putExtra("send_price", send_price);
i.putExtra("mobile", user_mobile);
i.putExtra("all_data", all_data);
i.putExtra("final_off", final_off);
i.putExtra("add", add);
startActivity(i);
finish();
} else {
Toast.makeText(getApplicationContext(),
R.string.wait_for_gps,
Toast.LENGTH_SHORT).show();
}
}
});
} // end Of OnCreate
public void findlocation()
{
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gps_enabled && !network_enabled) { Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "nothing is enabled", duration);
toast.show();
}
if (ContextCompat.checkSelfPermission(Maps2.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (gps_enabled) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
}
}else
{
ActivityCompat.requestPermissions(Maps2.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
if (network_enabled) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
}
timer=new Timer();
timer.schedule(new GetLastLocation(), 20000);
}
public void showlocation(double latitude, double longtitude)
{
LatLng position = new LatLng(latitude, longtitude);
// map.clear();
// Marker mark = map.addMarker(new MarkerOptions().position(position).title("موقعیت شما"));
//.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_location_icon));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 17.0f));
// animateMarker(position, false);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
lat =location.getLatitude();
lng = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
//Toast toast = Toast.makeText(context, "gps enabled "+x + "\n" + y, duration);
//toast.show();
showlocation(lat, lng);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
lat = location.getLatitude();
lng = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
showlocation(lat, lng);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc = null, gps_loc = null;
if (ContextCompat.checkSelfPermission(Maps2.this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} else {
ActivityCompat.requestPermissions(Maps2.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
//if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime()) {
lat = gps_loc.getLatitude();
lng = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
showlocation(lat, lng);
} else {
lat = net_loc.getLatitude();
lng = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
showlocation (lat, lng);
// Toast toast = Toast.makeText(context, "network lastknown " + x + "\n" + y, duration);
// toast.show();
}
}
if (gps_loc != null) {
{
lat = gps_loc.getLatitude();
lng = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown " + lat + "\n" + lng, duration);
toast.show();
}
}
if (net_loc != null) {
{
lat = net_loc.getLatitude();
lng = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
// Toast toast = Toast.makeText(context, "network lastknown " + x + "\n" + y, duration);
// toast.show();
}
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "no last know avilable", duration);
toast.show();
}
}
}
您可以使用
共享的首选项
保留当前位置。并且请清楚地写下您的问题,以便我们理解您的问题。