import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rajitha.model.Point;
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.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
public class CurrentLocationActivity extends AppCompatActivity implements
OnMapReadyCallback, LocationListener {
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private GoogleMap mMap;
ImageView share;
TextView logitude,latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar tb = findViewById(R.id.toolbar);
share=(ImageView)findViewById(R.id.share);
logitude=(TextView)findViewById(R.id.logitude);
latitude=(TextView)findViewById(R.id.latitude);
setSupportActionBar(tb);
tb.setSubtitle("Your Point");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.current_location);
mapFragment.getMapAsync(this);
LocationManager mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mLocListener = new CurrentLocationActivity();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Test share",logitude.getText().toString());
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMyLocationButtonClickListener(onMyLocationButtonClickListener);
mMap.setOnMyLocationClickListener(onMyLocationClickListener);
enableMyLocationIfPermitted();
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMinZoomPreference(5);
}
private void enableMyLocationIfPermitted() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
private void showDefaultLocation() {
Toast.makeText(this, "Point permission not granted, " +
"showing default location",
Toast.LENGTH_SHORT).show();
LatLng redmond = new LatLng(7.8731, 80.7718);
mMap.moveCamera(CameraUpdateFactory.newLatLng(redmond));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocationIfPermitted();
} else {
showDefaultLocation();
}
return;
}
}
}
private GoogleMap.OnMyLocationButtonClickListener onMyLocationButtonClickListener =
new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.setMinZoomPreference(5);
return false;
}
};
private GoogleMap.OnMyLocationClickListener onMyLocationClickListener =
new GoogleMap.OnMyLocationClickListener() {
@Override
public void onMyLocationClick(@NonNull Location location) {
mMap.setMinZoomPreference(4);
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(location.getLatitude(),
location.getLongitude()));
circleOptions.radius(200);
circleOptions.fillColor(Color.RED);
circleOptions.strokeWidth(6);
mMap.addCircle(circleOptions);
}
};
@Override
public void onLocationChanged(Location location) {
logitude.setText(String.valueOf(location.getLongitude()));
latitude.setText(String.valueOf(location.getLatitude()));
String message = String.format(
"New Point \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Log.e("Test",message);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
在这里,我只需要更新文本中的当前经度和纬度值。但是运行此代码后,我得到了一个空异常。似乎由于经onLocationChanged方法后的onCreate调用而使纬度和纬度TextViews变为空。我需要在单击按钮之后共享这些值。如何从上述代码中完成这些任务。在此先感谢。
答案 0 :(得分:1)
您注意到您的mLocListener是什么吗?
删除LocationListener mLocListener = new CurrentLocationActivity();
而不是 mLocListener 随处使用 this 。
喜欢变化
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);
到
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);