-1

我想在 googleMap 中显示纬度和经度。1. 获得 lat n lang 的最佳方式是什么?怎么做?2. Manifest 文件需要做哪些修改??请配合。

  public class MainActivity extends FragmentActivity{

protected GoogleMap gMap;
    String gotbread;     
TextView text;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (gMap == null) {
            Toast.makeText(this, "Google Maps not available",           
         Toast.LENGTH_LONG).show();}
    text=(TextView) findViewById(R.id.text1);


    gMap.setMyLocationEnabled(true);

  }    

     } 

第二类:取值的来源。

         public class Locate extends Activity implements LocationListener {

       Location location = null; // location
           double latitude; // latitude
            double longitude; // longitude
         TextView text1,text2;

@Override
       protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locate);


      text1=(TextView) findViewById(R.id.textView1);
              text2=(TextView) findViewById(R.id.textView2);

          location();
         }




      private void location()
   {
          try{


          final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

  // The minimum time between updates in milliseconds
      final long MIN_TIME_BW_UPDATES = 1000 * 60 * 5; // 5 minute

  // Declaring a Location Manager
          LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

                // getting network status
                boolean isNetworkEnabled1 = locationManager
                             .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                      if (isNetworkEnabled1) {
                             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();

                                       text1.setText(latitude+" ");
                                       text2.setText(longitude+" ");


                                    }
                                                  }
                                 }
               }catch(Exception e)
                    {
                      e.printStackTrace();
                    }




        }                          

    public void onStatusChanged(String provider, int status, Bundle extras) {}

       public void onProviderEnabled(String provider) {}

      public void onProviderDisabled(String provider) {}

      public void onLocationChanged(Location arg0) {}


     }

清单文件,

     <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.map"
     android:versionCode="1"
     android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

     <permission
    android:name="com.example.map.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

      <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission                android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.

-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.map.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>




       <activity
        android:name="com.example.map.Locate"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Locate" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
      </activity>



        <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBeJoGKnhxYps-youI1xfMZn6S2G05l0PM" />

      </application>

     </manifest>
4

1 回答 1

1

请参阅此链接以完善地图应用程序。

使用以下类获取当前的纬度和经度。

package com.map.example;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;

public class GPSTracker extends Service implements LocationListener{

private Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;
double latitude,longitude;

LocationManager locationManager;
AlertDialogManager am = new AlertDialogManager();
public GPSTracker(Context context){
    this.context = context;
    getLocation();
}
private Location getLocation() {
    // TODO Auto-generated method stub
    try{
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


        if (!isGPSEnabled && !isNetworkEnabled){

        } else {
            this.canGetLocation = true;

            if (isNetworkEnabled){

                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 3, this);

                if (locationManager != null){
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }

            if (isGPSEnabled){
                if (location == null){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 3, this);
                    if (locationManager != null){
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            } else {
                showAlertDialog();
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return location;
}


public void showAlertDialog(){
    am.showAlertDialog(GPSTracker.this, "GPS Setting", "Gps is not enabled. Do you want to enabled it ?", false);
}
public double getLatitude(){
    if (location != null){
        latitude = location.getLatitude();
    }

    return latitude;
}

public double getLongitude(){
    if (location != null){
        longitude = location.getLongitude();
    }

    return longitude;
}

public boolean canGetLocation(){
    return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    if (location != null){
        this.location = location;
    }
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

现在在您的主 Activity 中创建 GPSTracker 的实例,如下所示。

GPSTracker gps = new GPSTracker(context);
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();

//    Then pass this lattitude and longitude as shown below.
Intent intent = new Intent (this, yourSecondActivity.class)
intent.putExtra("latitude ",latitude );
intent.putExtra("longitude ",longitude );
startActivity(intent);
于 2013-09-18T08:59:22.240 回答