0

私の活動の1つでは、ジオコーディングを実行できる必要があります(住所文字列検索で場所を検索します)。問題は、私の結果が広すぎることです。「マクドナルド」を検索すると、米国のさまざまな地域にある結果が表示されます。ユーザーが近くのレストラン(または任意の場所)を検索でき、結果が特定の距離内に表示されるようにするにはどうすればよいですか?基本的に、アプリケーションにはより正確な結果が必要です。これが起こっていることのスクリーンショットです:ここに画像の説明を入力してください

public class MainActivity extends MapActivity {
HelloItemizedOverlay itemizedOverlay;
List<Overlay> mapOverlays;
Drawable drawable;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MapView myMap = (MapView) findViewById(R.id.mapview);
    myMap.setBuiltInZoomControls(true);

    MapController mc = myMap.getController();

    mapOverlays = myMap.getOverlays();
    drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    itemizedOverlay = new HelloItemizedOverlay(drawable, this);

    //geopoints are cordinates in microdegrees or degrees * E6
    GeoPoint point = new GeoPoint(34730300, -86586100);
    //GeoPoint point2 = locatePlace("texas", mc); //HelloItemizedOverlay.locatePlace("Texas", mc, myMap);

    //overlayitems are items that show the point of location to the user
    OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "im in huntsville");
    //OverlayItem overlayitem2 = new OverlayItem(point2, "Texas", "hi");

    //itemizedoverlay is used here to add a drawable to each of the points
    itemizedOverlay.addOverlay(overlayitem);
    //itemizedOverlay.addOverlay(overlayitem2);

    //this adds the drawable to the map

    //this method converts the search address to locations on the map and then finds however many you wish to see.
    locatePlace("mcdonalds", mc, 5);
    mapOverlays.add(itemizedOverlay);

    //this animates to the point desired (i plan on having "point" = current location of the user)
    mc.animateTo(point);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

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

public void locatePlace(String locName, MapController mc, int numberToDisplay)
{   // code to make the google search via string work

    // i use the Geocoder class is used to handle geocoding and reverse-geocoding. So make an instance of this class to work with the methods included
    Geocoder geoCoder1 = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> searchAddresses = geoCoder1.getFromLocationName(locName, numberToDisplay);  // gets a max of 5 locations
        if (searchAddresses.size() > 0)
            {
            //iterate through using an iterator loop (for loop would have been fine too)
            //Iterator<Address> iterator1 = searchAddresses.iterator();
            for (int i=0; i < searchAddresses.size(); i++){
            //while (iterator1.hasNext()){
                //step1 get a geopoint
                GeoPoint tempGeoP = new GeoPoint( (int) (searchAddresses.get(i).getLatitude()*1E6), (int) (searchAddresses.get(i).getLongitude()*1E6) );
                //step2 add the geopoint to the Overlay item 
                OverlayItem tempOverlayItm = new OverlayItem(tempGeoP, locName, "this is " + locName);
                //step3 add the overlay item to the itemized overlay
                HelloItemizedOverlay tempItemizedOverlay = new HelloItemizedOverlay(drawable, this);  // its breakking here.........
                tempItemizedOverlay.addOverlay(tempOverlayItm);
                //the itemized overlay is added to the map Overlay
                mapOverlays.add(tempItemizedOverlay);
            }

            }
        } catch (IOException e)
        {
            e.printStackTrace();
            // Log.e("the error", "something went wrong: "+ e);
        }//finally   {}
    }
}

//これがItemizedオーバーレイクラスの重要なコードです

public HelloItemizedOverlay(Drawable defaultMarker, Context context)
    {
        super(boundCenter(defaultMarker));
        myContext = context;
    }

    public void addOverlay(OverlayItem overlay){
        mOverlays.add(overlay);
        populate();
    }

ありがとう、アダム

Vishwaのコメントに合わせた後のコードは次のとおりです。

 // these 2 variables are my current location
    latitudeCurrent  = 34730300;   // make this dynamic later
    longitudeCurrent = -86586100;  // make this dynamic later


LLlatitude  = (latitudeCurrent - 100000)/(1E6); //lowerleft latitude = original lat - (1)*degree/10
LLlongitude = (longitudeCurrent+ 100000)/(1E6);//lowerleft longitude = original longitude - (1)*degree/10
URlatitude  = (latitudeCurrent + 100000)/(1E6); //upperright latitude = original + (1)*degree/10
URlongitude = (longitudeCurrent+ 100000)/(1E6); //upperright longitude = original longitude + (1)*degree/10

        try {
            List<Address> searchAddresses = geoCoder1.getFromLocationName(locName, numberToDisplay, LLlatitude, LLlongitude, URlatitude, URlongitude); 

getfromlocationname()から許容可能な結果の2乗を作成した後

4

1 に答える 1

1

必要な作業は次のとおりです。

1.) GPS を使用してユーザーの現在地を取得します。これを行うのに役立つ回答を次に示します。

2.) 次に、結果を表示する半径を決定します。つまり、ユーザーの場所から 2 マイル以内の結果が必要か、5 マイル以内の結果が必要かなどです。

3.) これ (ポイント 2 を参照) が緯度と経度の値に関して何に変換されるかを理解するため、基本的には、lowerLeftLatitude、lowerLeftLongitude、lowerRightLatitude、lowerRightLongitude で境界ボックスを定義する必要があります。これらの計算は、1 つの経度がマイルに変換される量を計算することによって行うことができます (繰り返しますが、この計算は、経度の機能の性質により、場所によって経度間の距離が変化するため、おそらく概算になります (これを読んでください: http://en.wikipedia.org/wiki/経度各人の位置に基づいて調整したい場合)

4.) 現在使用しているバージョンを使用する代わりに、次のメソッド (getFromLocationName のバリアントです) を使用します: http://developer.android.com/reference/android/location/Geocoder.html#getFromLocationName(java. lang.String、int、double、double、double、double)。この方法がどのように機能するかを読んでください。基本的には、名前 (つまり、マクドナルド) とは別に、バウンディング ボックスの値も指定します。そのため、ユーザーの場所に固有の結果が得られます。

状況を教えてください / さらにサポートが必要な場合

Places API から JSON 応答を取得するためのコードは次のとおりです。「 AddYourOwnKeyHere」を Places API キーに置き換えることを忘れないでください。

try
{
HttpPost httppost = new HttpPost("https://maps.googleapis.com/maps/api/place/search/json?location=34.730300,-86.586100&radius=19308&types=food&name=mcdonalds&sensor=false&key=AddYourOwnKeyHere");
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(data);
//Parse the JSONObject now
} catch (Exception e) {
e.printStackTrace();
}
于 2012-10-09T21:59:36.540 に答える