|
|
|
รบกวนหน่อยครับ Android Map โชว์พิกัดตัวเองได้แต่เรียกค่าโชว์จุดที่2 จาก databaseไม่ได้ |
|
|
|
|
|
|
|
Code (Android-Java)
package com.example.mylocation;
import java.text.NumberFormat;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.mylocation.Main;
import com.example.mylocation.R;
import com.example.mylocation.Main.MyItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ParseException;
import android.util.Log;
public class Main extends MapActivity {
LocationManager myLocationManager;
LocationListener myLocationListener;
MyLocationOverlay myLocationOverlay;
MapView mapView;
MapController mc;
TextView tv;
MyItemizedOverlay itemizedOverlay;
Drawable drawable;
List<Overlay> mapOverlays;
String latt;
String lonn ;
String imei2;
String bus;
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
mapView = (MapView) findViewById(R.id.mapview);
mc = mapView.getController();
mc.setZoom(15);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocationListener = new MyLocationListener();
// ตรวจสอบว่าเปิด GPS หรือไม่
if (!isGpsEnable()) {
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, myLocationListener);
myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.getOrientation();
myLocationOverlay.enableCompass();// แสดงเข็มทิศ
mapView.getOverlays().add(myLocationOverlay);// แสดงบนแผนที่
mapView.invalidate();
}
public boolean onCreateOptionsMenu(Menu main) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, main);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)// เมนูู
{
switch (item.getItemId()) {
case R.id.menuItem1:
Toast.makeText(Main.this, "เคลียร์สายรถประจำทาง", 3000).show();
return true;
case R.id.menuItem2:
Toast.makeText(Main.this, "เมนูแผนที่", Toast.LENGTH_SHORT).show();
if (mapView.isSatellite() == true) {
mapView.setSatellite(false);
}
return true;
case R.id.menuItem3:
Toast.makeText(Main.this, "แผนที่ดาวเทีนม", Toast.LENGTH_SHORT)
.show();
if (mapView.isSatellite() == false) {
mapView.setSatellite(true);
}
return true;
case R.id.menu_bus66:
Toast.makeText(Main.this, "สาย 66", 3000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Handle the back button
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setMessage("คุณต้องการออกจากโปรแกรม")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Main.this.finish();
}
}).setNegativeButton("No", null).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
public boolean isGpsEnable() {
boolean isgpsenable = false;
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.equals("")) { // GPS is Enabled
isgpsenable = true;
}
return isgpsenable;
}
// ///////// locationListener class ////////
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location argLocation) {
GeoPoint gpsPoint = new GeoPoint(
(int) (argLocation.getLatitude() * 1000000),
(int) (argLocation.getLongitude() * 1000000));
mc.animateTo(gpsPoint);
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(6);
String lat = nf.format(argLocation.getLatitude());
String lon = nf.format(argLocation.getLongitude());
tv.setText(latt + "," + lonn);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}// end locationListener class
public void LoadData() {
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.209.183/server/data.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// paring data
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
latt = json_data.getString("latt");
lonn = json_data.getString("lonn");
imei2 = json_data.getString("imei2");
bus = json_data.getString("bus");
List<Overlay> mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.bus);
itemizedOverlay = new MyItemizedOverlay(drawable);
double lat = Double.parseDouble(latt);
double lon = Double.parseDouble(lonn);
GeoPoint point = new GeoPoint((int) (lat * 1E6),
(int) (lon * 1E6));
OverlayItem overlayitem = new OverlayItem(point, "bus", imei2);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
mc = mapView.getController();
mc.setCenter(point);
tv.setText(latt + "," + lonn);
// Overlay code
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
// Customized MyItemizedOverlay class
class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
// Define an OverlayItem ArrayList, in which we'll put each of
// the OverlayItem objects we want on our map
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public int size() {
return mOverlays.size();
}
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
// Title
String title = item.getTitle();
// Snippet
String snippet = item.getSnippet();
Toast.makeText(Main.this, title + "\n" + snippet, Toast.LENGTH_LONG)
.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
}
}
Tag : MySQL, Android, JAVA
|
|
|
|
|
|
Date :
2013-01-31 02:18:37 |
By :
lonalosolo |
View :
1348 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ข้อมูลใน ไฟล์ php มีครับ โชว์แล้วได้ครับ แต่พอลงในมือถือมันก็เปิดไม่ได้ ครับ ทำไงดี เหมือนมันเรียกไม่ได้อะครับ
|
ประวัติการแก้ไข 2013-01-31 17:02:07
|
|
|
|
Date :
2013-01-31 17:01:09 |
By :
lonalosolo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|