0

我正在尝试通过 shop_name 从我的数据库中获取纬度和经度,以便我可以使用它从我的位置获取方向。

这是我的地图活动:

public class SearchLocation extends MapActivity {
private MapView mapView;
private LocationManager locManager;
private LocationListener locListener;

location_dao location_dao = new location_dao();

String shopname = "0", address = "0", phone_number = "0";

public CharSequence lat = "0", lng = "0", lat2 = "0", lng2 = "0";

LocationManager mlocManager;
LocationListener mlocListener;

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

    final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
    actionBar.setTitle(getString(R.string.app_name));

    actionBar.setHomeAction(new IntentAction(this, new Intent(this,
            Home_Activity.class), R.drawable.home));

    initMap();
    initLocationManager();
}

private void initMap() {
    mapView = (MapView) findViewById(R.id.map_view);
    mapView.setBuiltInZoomControls(true);
    mapView.displayZoomControls(true);
    mapView.getController().setZoom(15);
    mapView.setSatellite(false);

}

private void initLocationManager() {
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locListener = new LocationListener() {
        //method ini akan dijalankan apabila koordinat GPS berubah
        public void onLocationChanged(Location newLocation) {
            tampilkanPosisikeMap(newLocation);
        }

        public void onProviderDisabled(String arg0) {
        }

        public void onProviderEnabled(String arg0) {
        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locListener);

}

protected void tampilkanPosisikeMap(Location newLocation) {
    List overlays = mapView.getOverlays();

    // first remove old overlay
    if (overlays.size() > 0) {
        for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
            iterator.next();
            iterator.remove();
        }
    }

    // transform the location to a geopoint
    GeoPoint geopoint = new GeoPoint(
    (int) (newLocation.getLatitude() * 1E6), (int) (newLocation.getLongitude() * 1E6));

    // initialize icon
    Drawable icon = getResources().getDrawable(R.drawable.marker1);
    icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());

    // create my overlay and show it
    mapsOverlay overlay = new mapsOverlay(icon);
    OverlayItem item = new OverlayItem(geopoint, "My Location", null);
    overlay.addItem(item);
    mapView.getOverlays().add(overlay);

    // move to location
    mapView.getController().animateTo(geopoint);

    // redraw map
    mapView.postInvalidate();
}



public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(final Location loc) {
        SearchLocation.this.parsingGetServerLocationData(shopname);

        if (lat != "0" && lng != "0") {
            loc.getLatitude();

            loc.getLongitude();

            mlocManager.removeUpdates(this);
            lat2 = String.valueOf(loc.getLatitude());
            lng2 = String.valueOf(loc.getLongitude());

            Location bLocation = new Location("reverseGeocoded");
            bLocation.setLatitude(Double.parseDouble((String) lat2));
            bLocation.setLongitude(Double.parseDouble((String) lng2));
            Location aLocation = new Location("reverseGeocoded");
            aLocation.setLatitude(Double.parseDouble((String) lat));
            aLocation.setLongitude(Double.parseDouble((String) lng));

            Location myLocation = LocationHelper
                    .getLastLocation(getBaseContext());

            int distance = (int) aLocation.distanceTo(bLocation);

            // handler.sendEmptyMessage(0);

            // Toast.makeText(searchLocation.this, "Your distance: " +
            // distance + " Meter", Toast.LENGTH_SHORT).show();

            Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr="
                    + myLocation.getLatitude() + "%20"
                    + myLocation.getLongitude() + "&daddr=" + lat + "%20"
                    + lng + "&hl=en");
            Intent it = new Intent(Intent.ACTION_VIEW, uri);

            startActivity(it);
            lat = "0";
            lng = "0";
        } else {
            // handler.sendEmptyMessage(0);
            // Toast.makeText(searchLocation.this, "no data found",
            // Toast.LENGTH_LONG).show();
        }
    }

    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(),
                "Gps Disabled, Switch to Network", Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Enabled",
                Toast.LENGTH_SHORT).show();
    }

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

public void parsingGetServerLocationData(String shop_name) {
    try {
        JSONArray jArray = new JSONArray(
                location_dao.getServerLocationData(shop_name));
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            // Get an output to the screen
            shopname = json_data.getString("SHOP_NAME");
            lat = json_data.getString("LATITUDE");
            lng = json_data.getString("LONGITUDE");

            Log.e("log_tag",
                    "parsingGetServerLocationData" + shopname.toString());
        }

    } catch (JSONException e) {
        // TODO: handle exception
        Log.e("log_tag", "Error parsing data " + e.toString());
        shopname = "0";
        lat = "0";
        lng = "0";
    }

}

public void parsingGetServerDetailLocationData(String shop_name)
{
    try{
           JSONArray jArray = new JSONArray(location_dao.getServerDetailLocationData(shop_name));
           for(int i=0;i<jArray.length();i++){
               JSONObject json_data = jArray.getJSONObject(i);
               //Get an output to the screen                   
               shopname = json_data.getString("SHOP_NAME");
               address = json_data.getString("ADDRESS");
               phone_number = json_data.getString("PHONE_NUMBER");

               Log.e("log_tag", "parsingGetServerDetailLocationData: "+shopname.toString());
           }
       }catch(JSONException e){
           Log.e("log_tag", "Error parsing data "+e.toString());
           shopname = "0";
           address = "0";
           phone_number = "0";
       }
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}}

这是我调用地图活动的代码:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    final Entity_BikeShopRepair toko = adapterShop.getItem(position);

    CharSequence[] items = { "View on Map", "Call Shop" };

    AlertDialog.Builder builder = new AlertDialog.Builder(
            Tab_Shop_Repair_ListView_Activity.this);
    builder.setTitle(toko.getShop_Name());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            switch (item) {
            case 0:
                String nama = toko.getShop_Name().toString();

                Intent intentmap = new Intent(
                        Tab_Shop_Repair_ListView_Activity.this,
                        SearchLocation.class);
                startActivity(intentmap);
                break;
            case 1:
                String number = "tel:" + toko.getPhone_Number().toString();

                Intent intent = new Intent(Intent.ACTION_CALL, Uri
                        .parse(number));
                startActivity(intent);

                break;
            case 2:
                break;
            }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

在此处输入图像描述

这是我的日志:

10-02 22:40:32.859: E/AndroidRuntime(4375): android.view.WindowManager$BadTokenException: 无法添加窗口——令牌 android.os.BinderProxy@405139f0 无效;您的活动正在运行吗?10-02 22:40:32.859: E/AndroidRuntime(4375): 在 android.view.ViewRoot.setView(ViewRoot.java:532) 10-02 22:40:32.859: E/AndroidRuntime(4375): 在 android. view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 10-02 22:40:32.859: E/AndroidRuntime(4375): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 10-02 22:40: 32.859: E/AndroidRuntime(4375): 在 android.view.Window$LocalWindowManager.addView(Window.java:424) 10-02 22:40:32.859: E/AndroidRuntime(4375): 在 android.app.Dialog.show (Dialog.java:241) 10-02 22:40:32.859: E/AndroidRuntime(4375): 在 com.wilis.hellotabwidget.Tab_Shop_Repair_ListView_Activity$1。Katamso 210 -212","地址":"Jl. 布里金德。Katamso 210 -212","2":"0274375180","phone_number":"0274375180"},{"0":"Sinar Makmur","shop_name":"Sinar Makmur","1":"Jl. Brigjen Katamso 92B","地址":"Jl. Brigjen Katamso 92B","2":"0274378352","phone_number":"0274378352"},{"0":"Orion","shop_name":"Orion","1":"Jl Brigjen Katamso 56", "address":"Jl Brigjen Katamso 56","2":"0274374986","phone_number":"0274374986"},{"0":"Pasar Sepeda PPSY","shop_name":"Pasar Sepeda PPSY"," 1":"Jl. 部长苏佩诺,索罗苏坦","地址":"Jl. Menteri Supeno, Sorosutan","2":"-","phone_number":"-"},{"0":"Toko Sepeda Adi Mitra","shop_name":"Toko Sepeda Adi Mitra","1": “杰尔。Mayjen Sutoyo 09","地址":"Jl. Mayjen Sutoyo 09","2":"0274375199","phone_number":"0274375199"},{"0":"Pasar Sepeda GAPPSTA","shop_name":"Pasar Sepeda GAPPSTA","1":"Jl. 公吨。Haryono","地址":"Jl. 公吨。Haryono","2":"-","phone_number":"-"},{"0":"Pasar Sepeda Terban","shop_name":"Pasar Sepeda Terban","1":"Jl. C. Simanjuntak"," 地址":"Jl. C. Simanjuntak","2":"-","phone_number":"-"},{"0":"Toko Sepeda Marcopolo","shop_name":"Toko Sepeda Marcopolo","1":"Jl. Sosrowijayan No. 11","地址":"Jl. Sosrowijayan No. 11","2":"0274587636","phone_number":"0274587636"},{"0":"Toko Sepeda Esa Jaya","shop_name":"Toko Sepeda Esa Jaya","1": “杰尔。哈。Dahlan No 72","地址":"Jl. 哈。Dahlan No 72","2":"0274515605","phone_number":"0274515605"},{"0":"Toko Sepeda United","shop_name":"Toko Sepeda United","1":"Jl. P. Diponegoro 42", 地址":"Jl. P. Diponegoro 42","2":"0274518375","phone_number":"0274518375"},{"0":"Toko Sepeda Taman Sari Indah","shop_name":"Toko Sepeda Taman Sari Indah","1 ":"Jl.Wachid Hasyim 129","address":"Jl.Wachid Hasyim 129","2":"-","phone_number":"-"},{"0":"Toko Sepeda Salam", "shop_name":"Toko Sepeda Salam","1":"Jl.Wachid Hasyim 19","address":"Jl.Wachid Hasyim 19","2":"0274374913","phone_number":"0274374913"} ,{"0":"AB 自行车","shop_name":"AB 自行车","1":"Jl. HOS Cokroaminoto No.45","地址":"Jl. HOS Cokroaminoto No.45","2":"0274618027","phone_number":"0274618027"},{"0":"Indo Jaya Sakti","shop_name":"Indo Jaya Sakti","1":" Jl。Hos Cokroaminoto No. 157 B","address":"Jl. Hos Cokroaminoto No. 157 B","2":"0274620188","phone_number":"0274620188"},{"0":"Toko Sepeda Rodalink","shop_name":"Toko Sepeda Rodalink","1": “杰尔。Laskda Adisucipto Km 6 No. 155C","address":"Jl. Laskda Adisucipto Km 6 No. 155C","2":"0274487229","phone_number":"0274487229"},{"0":"Pedro Pit Shop (Pusat)","shop_name":"Pedro Pit Shop (Pusat) )","1": 549:E/ZoomButtonsController(4493):如果所有者视图未附加到窗口,则无法使缩放控制器可见。10-02 22:41:52.609:I/MapActivity(4493):处理网络更改通知:已连接

它从我的数据库中获取所有数据,但是当我单击时我只想显示一个商店名称。

10-02 22:41:44.609: E/HttpResponse(4493): {"result":1,"data":[{"0":"Toko Sepeda Alam Baru","shop_name":"Toko Sepeda Alam Baru","1":"Jl. Malioboro No. 151","address":"Jl. Malioboro No. 151","2":"0274514903","phone_number":"0274514903"},{"0":"Toko Sepeda Tri Jaya","shop_name":"Toko Sepeda Tri Jaya","1":"Jl. Bridjend. Katamso 210 -212","address":"Jl. Bridjend. Katamso 210 -212","2":"0274375180","phone_number":"0274375180"},{"0":"Sinar Makmur","shop_name":"Sinar Makmur","1":"Jl. Brigjen Katamso 92B","address":"Jl. Brigjen Katamso 92B","2":"0274378352","phone_number":"0274378352"},{"0":"Orion","shop_name":"Orion","1":"Jl Brigjen Katamso 56","address":"Jl Brigjen Katamso 56","2":"0274374986","phone_number":"0274374986"},{"0":"Pasar Sepeda PPSY","shop_name":"Pasar Sepeda PPSY","1":"Jl. Menteri Supeno, Sorosutan","address":"Jl. Menteri Supeno, Sorosutan","2":"-","phone_number":"-"},{"0":"Toko Sepeda Adi Mitra","shop_name":"Toko Sepeda Adi Mitra","1":"Jl. Mayjen Sutoyo 09","address":"Jl. Mayjen Sutoyo 09","2":"0274375199","phone_number":"0274375199"},{"0":"Pasar Sepeda GAPPSTA","shop_name":"Pasar Sepeda GAPPSTA","1":"Jl. MT. Haryono","address":"Jl. MT. Haryono","2":"-","phone_number":"-"},{"0":"Pasar Sepeda Terban","shop_name":"Pasar Sepeda Terban","1":"Jl. C. Simanjuntak","address":"Jl. C. Simanjuntak","2":"-","phone_number":"-"},{"0":"Toko Sepeda Marcopolo","shop_name":"Toko Sepeda Marcopolo","1":"Jl. Sosrowijayan No. 11","address":"Jl. Sosrowijayan No. 11","2":"0274587636","phone_number":"0274587636"},{"0":"Toko Sepeda Esa Jaya","shop_name":"Toko Sepeda Esa Jaya","1":"Jl. KHA. Dahlan No 72","address":"Jl. KHA. Dahlan No 72","2":"0274515605","phone_number":"0274515605"},{"0":"Toko Sepeda United","shop_name":"Toko Sepeda United","1":"Jl. P. Diponegoro 42","address":"Jl. P. Diponegoro 42","2":"0274518375","phone_number":"0274518375"},{"0":"Toko Sepeda Taman Sari Indah","shop_name":"Toko Sepeda Taman Sari Indah","1":"Jl.Wachid Hasyim 129","address":"Jl.Wachid Hasyim 129","2":"-","phone_number":"-"},{"0":"Toko Sepeda Salam","shop_name":"Toko Sepeda Salam","1":"Jl.Wachid Hasyim 19","address":"Jl.Wachid Hasyim 19","2":"0274374913","phone_number":"0274374913"},{"0":"AB Bike","shop_name":"AB Bike","1":"Jl. HOS Cokroaminoto No.45","address":"Jl. HOS Cokroaminoto No.45","2":"0274618027","phone_number":"0274618027"},{"0":"Indo Jaya Sakti","shop_name":"Indo Jaya Sakti","1":"Jl. Hos Cokroaminoto No. 157 B","address":"Jl. Hos Cokroaminoto No. 157 B","2":"0274620188","phone_number":"0274620188"},{"0":"Toko Sepeda Rodalink","shop_name":"Toko Sepeda Rodalink","1":"Jl. Laskda Adisucipto Km 6 No. 155C","address":"Jl. Laskda Adisucipto Km 6 No. 155C","2":"0274487229","phone_number":"0274487229"},{"0":"Pedro Pit Shop (Pusat)","shop_name":"Pedro Pit Shop (Pusat)","1":"Jln. Kebon Agung Km 4,6 Los Danon, Godean","address":"Jln. Kebon Agung Km 4,6 Los Danon, Godean","2":"0816972823","phone_number":"0816972823"},{"0":"Pedro Pit Shop (Cabang)","shop_name":"Pedro Pit Shop (Cabang)","1":"Godean Km 9, Godean","address":"Godean Km 9, Godean","2":"02747171665","phone_number":"02747171665"},{"0":"Pedro Pit Shop (Cabang)","shop_name":"Pedro Pit Shop (Cabang)","1":"Jln. Lingkar Utara (ringroad), Karangnongko, Maguwo, Depok, Sleman","address":"Jln. Lingkar Utara (ringroad), Karangnongko, Maguwo, Depok, Sleman","2":"085729075277","phone_number":"085729075277"},{"0":"Nurhayati Bike","shop_name":"Nurhayati Bike","1":"Jalan Raya Cebongan Sleman","address":"Jalan Raya Cebongan Sleman","2":"-","phone_number":"-"},{"0":"Love Sepeda","shop_name":"Love Sepeda","1":"Gandok, Gang suryo No.16, Condongcatur, Depok, Sleman","address":"Gandok, Gang suryo No.16, Condongcatur, Depok, Sleman","2":"085743919166","phone_number":"085743919166"}]}

如何通过 shop_name 获取纬度和经度?我在代码中做错了什么,请帮助我。谢谢你。

4

1 回答 1

0

您可以查看Geocoder.getFromLocationName()( javadoc ),您可以将您在 JSON 结构中的商店地址尽可能多地传递给其中。

检索到的Address对象具有可以传递给地图的纬度和经度。

于 2012-10-02T19:25:26.963 回答