
我有以下需要包含在Android应用程序中,我希望有人可以提供一些编码示例来说明我在这里尝试实现的目标.
> AndroID App在屏幕上有一个Google Maps界面.
>应用程序的用户可以浏览打开的Google Map界面并选择特定位置.
>我想获取此位置的GPS坐标.
提前致谢!!
解决方法:
在这里,答案.它工作正常.
我也发现这个解决方案来自堆栈溢出有一些问题.但现在所有解决方案和代码都运行良好.
为清单添加权限
AndroIDManifest.xml中
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="sichlearning.example.com.androIDgpstrackingactivity"> <uses-permission androID:name="androID.permission.ACCESS_COARSE_LOCATION" /> <uses-permission androID:name="androID.permission.INTERNET" /> <uses-permission androID:name="androID.permission.ACCESS_FINE_LOCATION" /> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <Meta-data androID:name="com.Google.androID.geo.API_KEY" androID:value="@string/Google_maps_key" /> <activity androID:name=".MapsActivity" androID:label="@string/Title_activity_maps"></activity> </application></manifest>activity_main.xml中
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <button androID:ID="@+ID/btnShowLocation" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="Show Location" androID:layout_gravity="center_horizontal" androID:layout_margintop="50dp" /></linearLayout>MainActivity.java
import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity { button btnShowLocation; // GPSTracker class GPSTracker gps; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); btnShowLocation = (button) findVIEwByID(R.ID.btnShowLocation); // Show location button click event btnShowLocation.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw arg0) { // Create class object gps = new GPSTracker(MainActivity.this); // Check if GPS enabled if(gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new line Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else { // Can't get location. // GPS or network is not enabled. // Ask user to enable GPS/network in settings. gps.showSettingsAlert(); } } }); } }GPSTracker.java
import androID.Manifest; import androID.app.AlertDialog; import androID.app.Service; import androID.content.Context; import androID.content.DialogInterface; 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.provIDer.Settings; import androID.support.v4.app.ActivityCompat;public class GPSTracker extends Service implements LocationListener { private final Context mContext; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location; // location double latitude; // latitude double longitude; // longitude // The minimum distance to change Updates in meters private static final long MIN_disTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; public GPSTracker(Context context) { this.mContext = context; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProvIDerEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProvIDerEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provIDer is enabled } else { this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_disTANCE_CHANGE_FOR_UPDATES, this); // Log.d("Network", "Network"); if (locationManager != null) { location = locationManager.getLastKNownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_disTANCE_CHANGE_FOR_UPDATES, this); // Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { 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 Todo; } location = locationManager .getLastKNownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { } return location; } /** * Stop using GPS Listener Calling this function will stop using GPS in your * app. * */ public voID stopUsingGPS() { if (locationManager != null) { 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.removeUpdates(GPSTracker.this); } } /** * Function to get latitude * */ public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitude return latitude; } /** * Function to get longitude * */ public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitude return longitude; } /** * Function to check GPS/wifi enabled * * @return boolean * */ public boolean canGetLocation() { return this.canGetLocation; } /** * Function to show settings alert dialog On pressing Settings button will * lauch Settings Options * */ public voID showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // 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); mContext.startActivity(intent); } }); // on pressing cancel button alertDialog.setNegativebutton("Cancel", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } @OverrIDe public voID onLocationChanged(Location location) { try { float bestAccuracy = -1f; if (location.getAccuracy() != 0.0f && (location.getAccuracy() < bestAccuracy) || bestAccuracy == -1f) { 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 01user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. // return ; } locationManager.removeUpdates(this); } bestAccuracy = location.getAccuracy(); } catch (NullPointerException e) { System.out.print("Caught the NullPointerException"); } } @OverrIDe public voID onProvIDerDisabled(String provIDer) { } @OverrIDe public voID onProvIDerEnabled(String provIDer) { } @OverrIDe public voID onStatusChanged(String provIDer, int status, Bundle extras) { } @OverrIDe public IBinder onBind(Intent arg0) { return null; } public float getAccurecy() { return location.getAccuracy(); }} 总结 以上是内存溢出为你收集整理的Android Studio:在Google地图中获取当前标记位置的GPS坐标全部内容,希望文章能够帮你解决Android Studio:在Google地图中获取当前标记位置的GPS坐标所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)