- package com.xin.mygooglemap;
- import android.Manifest;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.Uri;
- import android.os.Build;
- import android.provider.Settings;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private LocationManager manager;
- private final int ACCESS_FINE_LPCATION = 1678;
- private String best;
- private Location currentLocation;
- private LocationListener listener = new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- //when location change
- currentLocation = location;
- updatePosition();
- }
- @Override
- public void onStatusChanged(String s, int i, Bundle bundle) {
- }
- @Override
- public void onProviderEnabled(String s) {
- }
- @Override
- public void onProviderDisabled(String s) {
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- manager = (LocationManager) getSystemService(LOCATION_SERVICE);
- if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {//check Location permissions is open .
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage("is Enabled GPS ? ")
- .setTitle("Message")
- .setPositiveButton("yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
- }
- })
- .setNegativeButton("No", null)
- .create()
- .show();
- }
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LPCATION);
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(this, "have open Location Managetion !!", Toast.LENGTH_SHORT);
- } else {
- finish();
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- best = manager.getBestProvider(new Criteria(), true);//get best Result
- try {
- if (manager != null) {
- currentLocation = manager.getLastKnownLocation(best);//get last Read Location
- manager.requestLocationUpdates(best,5000,5,listener);//自動更新 5sec更新一次,5m以上更新
- }else{
- currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,5,listener);
- }
- }catch (SecurityException ex){
- Log.d("","GPS permission has not been activated");
- }
- updatePosition();
- }
- private void updatePosition() {
- TextView LocationText = findViewById(R.id.locationText);
- if(currentLocation!=null){
- //output
- LocationText.setText(String.format("Latitude: %f ,%n Longitude: %f,%n Altitude:" +
- " %f",currentLocation.getLatitude(),currentLocation.getLongitude(),
- currentLocation.getAltitude()));
- }else{
- LocationText.setText("Reading .....");
- }
- }
- public void openMap(View view) {
- Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(String.format("geo:%f, %f",currentLocation.getLatitude(),currentLocation.getLongitude())));
- startActivity(intent);
- }
- }
2018年5月21日 星期一
(app)實作定位個人位置
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。