4
package com.androidhive.googlemaps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource.OnLocationChangedListener;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class AndroidGoogleMapsActivity extends MapActivity {


private Button login;
private EditText username, password;
private GoogleMap mMap;
private OnLocationChangedListener mListener;
private LocationManager locationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // Displaying Zooming controls
    MapView mapView = (MapView) findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);

    /**
     * Changing Map Type
     * */
     mapView.setSatellite(true); // Satellite View
     mapView.setStreetView(true); // Street View
     mapView.setTraffic(true); // Traffic view


    /**
     * showing location by Latitude and Longitude
     * */        
    MapController mc = mapView.getController();
    double lat = Double.parseDouble("48.85827758964043");
    double lon = Double.parseDouble("2.294543981552124");
    GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
    mc.animateTo(geoPoint);
    mc.setZoom(15);
    mapView.invalidate(); 


    /**
     * Placing Marker
     * */
    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
    AddItemizedOverlay itemizedOverlay = 
         new AddItemizedOverlay(drawable, this);


    OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");

    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);

}



@Override
protected boolean isRouteDisplayed() {
    return false;
}

我是android的初学者。我没有在哪里包含从服务器数据中检索的车辆列表代码。车辆列表意味着当我打开地图时,它将提供有关车辆从服务器检索的规范。还想知道地图中的当前位置。教我这个代码的最佳解决方案。提前致谢。

4

1 回答 1

0
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.GpsSatellite;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.GpsStatus;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    String TAG = "GPS Location";
    private TextView myLongitude;
    private TextView myAddress ;
    private TextView myLatitude;

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

              myLatitude = (TextView)findViewById(R.id.mylatitude);
              myLongitude = (TextView)findViewById(R.id.mylongitude);
              myAddress = (TextView)findViewById(R.id.myaddress);





        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new MyLocationListener();  
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);


    }

    class MyLocationListener implements LocationListener{



        @Override
        public void onLocationChanged(Location loc) {
            System.out.println("location change");


             Toast.makeText(
                        getBaseContext(),
                        "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                            + loc.getLongitude(), Toast.LENGTH_LONG).show();
                String longitude = "Longitude: " + loc.getLongitude();
                Log.v(TAG, longitude);
                myLongitude.setText(longitude);
                String latitude = "Latitude: " + loc.getLatitude();
                Log.v(TAG, latitude);
                myLatitude.setText(latitude);
                /*-------to get City-Name from coordinates -------- */
                String cityName = null;

                Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
              // List<Address> addresses;

                try {
                      List<Address> addresses = gcd.getFromLocation(loc.getLatitude(),
                                loc.getLongitude(), 1);

                      if(addresses != null) {
                       Address returnedAddress = addresses.get(0);
                       StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                       }
                       myAddress.setText(strReturnedAddress.toString());
                       Toast.makeText(getBaseContext(), strReturnedAddress.toString(),Toast.LENGTH_LONG).show();
                      }
                      else{
                     //  myAddress.setText("No Address returned!");
                      }
                     } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                     // myAddress.setText("Canont get Address!");
                     }

               /* try {
                    addresses = gcd.getFromLocation(loc.getLatitude(),
                            loc.getLongitude(), 1);

                    if (addresses.size() > 0)
                        System.out.println(addresses.get(0).getLocality());

                    cityName = addresses.get(0).getLocality();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
                    + cityName;
                Toast.makeText(
                        getBaseContext(),"location:"+s, Toast.LENGTH_LONG).show();*/



        }

        @Override
        public void onProviderDisabled(String arg0) {
            System.out.println("location disable");
            Toast.makeText(MainActivity.this, " location  disable", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onProviderEnabled(String arg0) {
            System.out.println("location enable");
            Toast.makeText(MainActivity.this, " location  enable", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            System.out.println("location status change");
             Toast.makeText(MainActivity.this, " location status change.....", Toast.LENGTH_LONG).show();
        }

        private double distance(double lat1, double lon1, double lat2, double lon2) {
              double theta = lon1 - lon2;
              double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
              dist = Math.acos(dist);
              dist = rad2deg(dist);
              dist = dist * 60 * 1.1515;
               return (dist);
            }

           private double deg2rad(double deg) {
              return (deg * Math.PI / 180.0);
            }
           private double rad2deg(double rad) {
              return (rad * 180.0 / Math.PI);
            }

        }

    }

和 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >


    <TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Location"
   android:background="#505050"
   />
<TextView
   android:id="@+id/mylatitude"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:id="@+id/mylongitude"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Address"
   android:background="#505050"
   />
<TextView
   android:id="@+id/myaddress"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />

</LinearLayout>

使用此权限

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

确保在您的硬件设备上使用gps 。

于 2013-07-24T07:23:35.723 回答