0

在我的一项活动中,我需要能够进行地理编码(通过地址字符串搜索查找位置)。问题是我的结果太宽泛了。当我搜索“麦当劳”时,我会得到美国不同地区的结果。我怎样才能使用户可以搜索附近的餐馆(或任何位置)并且结果将在一定距离内?我的应用程序基本上需要更精确的结果。这是正在发生的事情的屏幕截图:在此处输入图像描述

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() 创建一个允许结果的平方后

4

1 回答 1

1

这是您需要做的:

1.) 使用 GPS 获取用户的当前位置,这是一个可以帮助您做到这一点的答案。

2.) 接下来,确定您希望在什么半径范围内显示您的结果,即您是否需要在用户位置 2 英里范围内或 5 英里等范围内的结果。

3.) 弄清楚这(参考第 2 点)在纬度和经度值方面的含义,所以基本上你必须定义一个具有 lowerLeftLatitude、lowerLeftLongitude、lowerRightLatitude、lowerRightLongitude 的边界框。这些计算可以通过计算一个经度转换成英里的多少来完成(同样,这个计算可能是近似的,因为经度之间的距离会因经度的工作方式而变化(阅读此:http://en.wikipedia.org/wiki/Longitude如果您希望根据每个人的位置进行调整)

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 回答