该程序之前正在运行。但现在我不能再获得位置信息了。位置值始终为null。我很高兴得到你的帮助!非常感谢你!
mainActivity.jave
package com.example.a12086.map;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private GPS gps;
private Location mlocation;
private EditText input;
double longitude;
double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
input = (EditText) findViewById(R.id.Edittext);
gps=new GPS(getApplicationContext());
mlocation=gps.getLocation();
if(mlocation==null)
{
Toast.makeText(getApplicationContext(),"something wrong",Toast.LENGTH_LONG).show();
}
else{
latitude=mlocation.getLatitude();
longitude=mlocation.getLongitude();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng Myposition = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(Myposition).title("My Position"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Myposition));
}
}
GPS.java
package com.example.a12086.map;
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.ContextCompat;
public class GPS extends Service implements LocationListener {
private final Context context;
boolean isGPSEnabled=false;
boolean isNETWORKEnable=false;
boolean canGetLocation=false;
Location location;
protected LocationManager locationManager;
public GPS (Context context){
this.context=context;
}
public Location getLocation(){
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
isNETWORKEnable = locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER);
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
if (locationManager == null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
if (location == null) {
if (isNETWORKEnable) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
if (locationManager == null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
}
}catch(Exception ex){
}
return location;
}
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public IBinder onBind(Intent arg0){
return null;
}
}
logcat的
04-27 17:51:42.930 6663-6854/com.example.a12086.map I/OpenGLRenderer: Initialized EGL, version 1.4
04-27 17:51:42.935 6663-6854/com.example.a12086.map W/linker: /vendor/lib64/libhwuibp.so: unused DT entry: type 0xf arg 0xe3a
04-27 17:51:42.949 6663-6830/com.example.a12086.map E/BufferQueueProducer: [SurfaceTexture-0-6663-0] connect: already connected (cur=1 req=1)
04-27 17:51:42.962 6663-6854/com.example.a12086.map E/OpenGLRenderer: allen debug liyu Key: 0
04-27 17:51:42.965 6663-6854/com.example.a12086.map E/OpenGLRenderer: allen debug liyu Key: 5242945
04-27 17:51:42.966 6663-6854/com.example.a12086.map E/OpenGLRenderer: allen debug liyu Key: 5242944
04-27 17:51:42.989 6663-6854/com.example.a12086.map E/OpenGLRenderer: allen debug liyu Key: 1
04-27 17:51:43.026 6663-6854/com.example.a12086.map E/OpenGLRenderer: allen debug liyu Key: 824633720832
04-27 17:51:44.873 6663-6836/com.example.a12086.map W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
04-27 17:51:44.885 6663-6836/com.example.a12086.map I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2
04-27 17:51:44.885 6663-6836/com.example.a12086.map I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 2
04-27 17:52:16.559 6663-6663/com.example.a12086.map W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
04-27 17:53:03.234 6663-6663/com.example.a12086.map W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
04-27 17:53:03.240 6663-6830/com.example.a12086.map E/BufferQueueProducer: [SurfaceTexture-0-6663-1] connect: already connected (cur=1 req=1)
04-27 17:53:04.791 6663-6663/com.example.a12086.map W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
04-27 17:53:04.791 6663-6663/com.example.a12086.map W/IInputConnectionWrapper: finishComposingText on inactive InputConnection