2013-03-08 4 views
6

6人のフーターすべての正確な緯度/経度(10進数表記)座標を見つけるための準備作業を行ったウィスコンシンのレストランの位置。これらの座標値を別のクラスの配列に格納するつもりです。また、ユーザーの現在のGPS位置を取得するために、コード内に位置リスナーを既に持っています。 (私はここで助けて欲しいGPS利用者にフーターズのレストランが現在の位置に最も近い緯度と経度の座標を比較する必要がある

package sam.finalmap.hooters; 


// Camera is the view of the map. 

import com.google.android.gms.maps.CameraUpdateFactory; 
// the google map 
import com.google.android.gms.maps.GoogleMap; 


import android.app.Activity; 
import android.content.Context; 

import android.graphics.Color; // for drawing a line. 

import android.location.Location; // for detecting location changes with the GPS. 
import android.location.LocationListener; // to listen for location changes 
import android.location.LocationManager; 
import android.os.Bundle; 

import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.maps.MapFragment; // the Map class. 
import com.google.android.gms.maps.model.LatLng; // for creating lattitudes and longitutes in memory. 
import com.google.android.gms.maps.model.Polyline; // used to draw from one location to the other 
import com.google.android.gms.maps.model.PolylineOptions; 

/** 
* Draws a map, uses GPS to get the current location, the draws a line from Eau CLaire (see constants) 
* to the new position, which will be the closest Hooters restaurant to the user's current location. 
* This is the AdapterView. 
* 
* @author daviddalsveen 
* 
*/ 
public class GMapsLocationPath extends Activity implements LocationListener { 
    /** Called when the activity is first created. */ 

    private GoogleMap mMap; 

    // constants to hard code all 6 of Wisconsin's Hooters restaurant points on the map: 

    private static final float Appleton_LAT = 44.2655012f; 

    private static final float Appleton_LNG = -88.4768057f; 


    private static final float Brookfield_LAT = 43.03645f; 

    private static final float Brookfield_LNG = -88.124937f; 


    private static final float EastMadison_LAT = 43.132432f; 

    private static final float EastMadison_LNG = -89.3016256f; 


    private static final float GreenBay_LAT = 44.477903f; 

    private static final float GreenBay_LNG = -88.067014f; 


    private static final float Janesville_LAT = 42.7215666f; 

    private static final float Janesville_LNG = -88.9889661f; 


    private static final float LaCrosse_LAT = 43.8109318f; 

    private static final float LaCrosse_LNG = -91.2536215f; 



    private LocationManager locationManager; 

    private TextView tv; // a Textview for displaying lattitude and longitude. 

    private float curLat = 44.88f; // current position -- assigned constants for 
            // testing... 
    private float curLng = -91.47f; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // called when the activity is first started. 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // recommended method by google to make the map object. 
     setUpMapIfNeeded(); 

     // Sets the map type to be "normal" 
     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     tv = (TextView) findViewById(R.id.label1); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       500, 1, this); 
     Location location = locationManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     // 500 is the minimum time interval to update, in milliseconds 
     // 1 is the distance in meters in which to sense an update. 
     // 'this' is the pending intent. 

     // center latitude and longitude for EC 
     float lat = Appleton_LAT; 
     float lng = Appleton_LNG; 

     // debug example... 
     Toast.makeText(this, "" + (int) (lat * 1E6), Toast.LENGTH_LONG).show(); 

     if (location == null) { // no last known location 
      locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, 
        this, null); 
      // Create a new Lattitude Longitude Object, passing it the 
      // coordinates. 
      LatLng latLng = new LatLng(lat, lng); 

      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.0f)); 
      // re-draw 

     } else { 
      // explicitly call and update view with last known location or the 
      // one set above. 
      onLocationChanged(location); 
     } 

    } 

    /** 
    * Checks to see that the map exists, if not, creates one. 
    */ 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the 
     // map. 
     if (mMap == null) { 
      mMap = ((MapFragment) getFragmentManager().findFragmentById(
        R.id.map)).getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       // The Map is verified. It is now safe to manipulate the map. 

      }// else? 
     } 
    } 

    // Java Interface RULE NOTE: that we must implement every method of 
    // interface LocationListener, 
    // whether we use the method or not. 
    /** 
    * Use the GPS to get the current location of the user 
    * 
    */ 
    public void onLocationChanged(final Location loc) { 

     String lat = String.valueOf(loc.getLatitude()); 
     String lon = String.valueOf(loc.getLongitude()); 
     Log.e("GPS", "location changed: lat=" + lat + ", lon=" + lon); 
     tv.setText("lat=" + lat + ", lon=" + lon); 

     curLat = Float.parseFloat(lat); // update the current lattitude and longitude. 
     curLng = Float.parseFloat(lon); 

     // Create a new Lattitude Longitude Object, passing it the coordinates. 
     LatLng latLng = new LatLng(curLat, curLng); 

     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.0f)); 
     // re-draw 
     draw(); 

    } 

    public void onProviderDisabled(String loc) { 
     Log.e("GPS", "provider disabled " + loc); 
    } 

    public void onProviderEnabled(String loc) { 
     Log.e("GPS", "provider enabled " + loc); 
    } 

    /** 
    * loc: name of location provider status: status of location provider 
    * (temporarily unavailable, etc) extras: optional bundle with additional 
    * status information 
    */ 
    public void onStatusChanged(String loc, int status, Bundle extras) { 
     Log.e("GPS", "status changed to " + loc + " [" + status + "]"); 
    } 

    /** 
    * Draw a line from 
    */ 
    public void draw() { 

     float lat = 44.88f; 
     float lng = -91.48f; 

     // Instantiates a new Polyline object and adds points to define a 
     // endpoints of a line 
     PolylineOptions rectOptions = new PolylineOptions().add(
       new LatLng(curLat, curLng)) 

       .add(new LatLng(lat, lng)); // Closes the polyline. 

     // Set the rectangle's color to red 
     rectOptions.color(Color.RED); 

     // Get back the mutable Polyline 
     Polyline polyline = mMap.addPolyline(rectOptions);  

    } 

} 

は、配列を解析する方法を見つけることですし、6つの店舗の場所のそれぞれとユーザの位置の違いを比較し、どちらの違いが最小である:以下の私のコードを参照してください。どのレストランの場所がユーザーに最も近いか)は、選択され、誰の情報が表示されるレストランです。

つまり、配列の解析が終了して6つの緯度と経度の差がすべて得られたら、最小の差異を使用するように指示するにはどうすればよいですか?

/** 
    * My teacher suggested subtracting the current latitudes and longitudes from the restaurant latitudes and 
    * longitudes to see if they fall within a certain range (lets just say less than 10). Then, using the resulting 
    * differences as absolute values in an if statement (if absolute value < 10 for both are true), that restaurant 
    * would be the one selected: 
    */ 

    //float[] H_Latitude = {44.2655012f, 43.03645f, 43.132432f, 44.477903f, 42.7215666f, 43.8109318f}; 

    //float[] H_Longitude = {-88.4768057f, -88.124937f, -89.3016256f, -88.067014f, -88.9889661f, -91.2536215f}; 

    float LATdifference = curLat - H_Latitude; 

    float LNGdifference = curLng - H_Longitude;//I'm pretty sure I can't use "H_Longitude and H_Latitude", because 
    //they're merely the name of the array. So how do I access the elements inside of them? How do I successfully 
    //address them with a reference variable that I can use to dynamically subtract from curLat and curLng and get 
    //what I need to replace the "i" in the for loops: 
              for (float LATdifference = 0; i < 4; i++) { 
               System.out.println (count[i]); 
                } 
+3

+1おそらく最も良いstackoverflow質問です。 –

+0

だから...多くの...コメント... –

+0

あなたはGPSのポイントを配列に入れて静的なintを使って配列にインデックスを付けることを考えましたか?何回も繰り返されるコードよりも、配列をループする方が簡単です。私はあなたのコードをきれいにする方法を各店舗に結びつけることができると思います。 :P – JustinDanielson

答えて

1

あなたはGPSがGoogle行き方API座標に供給し、最も近い店舗を決定するための走行距離を使用することができます。

Android Locationクラスには、2つのGPSポイント間の直線距離を取得するためのdistanceToメソッドまたはdistanceBetweenメソッドがあります。これを使って2〜3人の候補者に絞ってから、apiを使って最終的な答えを得ることができます。

関連する問題