Issue
i'm trying to get latitude and longitude by shop_name from my database so i can use it to get direction from my location.
here's my map activity :
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;
}}
and here's my code to call map activity :
@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();
}
here's my logcat:
10-02 22:40:32.859: E/AndroidRuntime(4375): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405139f0 is not valid; is your activity running? 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.view.ViewRoot.setView(ViewRoot.java:532) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.view.Window$LocalWindowManager.addView(Window.java:424) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.app.Dialog.show(Dialog.java:241) 10-02 22:40:32.859: E/AndroidRuntime(4375): at com.wilis.hellotabwidget.Tab_Shop_Repair_ListView_Activity$1.gotResult(Tab_Shop_Repair_ListView_Activity.java:75) 10-02 22:40:32.859: E/AndroidRuntime(4375): at com.wilis.dao.bike_shop_dao$1.gotResult(bike_shop_dao.java:84) 10-02 22:40:32.859: E/AndroidRuntime(4375): at com.wilis.connections.ConnectionHandler.handleMessage(ConnectionHandler.java:68) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.os.Handler.dispatchMessage(Handler.java:99) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.os.Looper.loop(Looper.java:130) 10-02 22:40:32.859: E/AndroidRuntime(4375): at android.app.ActivityThread.main(ActivityThread.java:3687) 10-02 22:40:32.859: E/AndroidRuntime(4375): at java.lang.reflect.Method.invokeNative(Native Method) 10-02 22:40:32.859: E/AndroidRuntime(4375): at java.lang.reflect.Method.invoke(Method.java:507) 10-02 22:40:32.859: E/AndroidRuntime(4375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 10-02 22:40:32.859: E/AndroidRuntime(4375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 10-02 22:40:32.859: E/AndroidRuntime(4375): at dalvik.system.NativeStart.main(Native Method) 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"}]} 10-02 22:41:50.749: D/PhoneWindow(4493): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@4053e818 has no id. 10-02 22:41:52.549: E/ZoomButtonsController(4493): Cannot make the zoom controller visible if the owner view is not attached to a window. 10-02 22:41:52.609: I/MapActivity(4493): Handling network change notification:CONNECTED
it's get all data from my database, but i want to show just one shopname when i click.
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"}]}
how can i get latitude and longitude by shop_name? what i am doing mistake in my code please help me. thank you before.
Solution
You may have a look at Geocoder.getFromLocationName()
(javadoc), to which you pass as much as possible of the shop address you have in your JSON structure.
The retrieved Address
object(s) have latitude and longitude that you can pass to the maps.
Answered By - Heiko Rupp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.