|
|
|
Android - ปัดหมุด Map v2 จาก JSON ครับ // ไม่ขึ้น marker ตำแหน่งที่ดึงมาจาก JSON แต่ขึ้นตำแหน่งปัจจุบัน |
|
|
|
|
|
|
|
จากที่เคยเห็น admin ตอบเรื่องปักหมุดจาก JSON อะครับ ลองแก้ๆดู มันขึ้นแค่หมุดของตำแหน่งปัจจุบันของเรา
ค่าที่เรา get มาเป็น double ของ lat กับ lng มันไม่ขึ้นหมุดอะครับ
Code (Android-Java)
package th.ac.src.project.finder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class mapTest extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, OnClickListener {
GoogleMap mMap;
Marker mMarker;
Polyline direction;
LatLng coordinate;
LocationClient mLocationClient;
Location mCurrentLocation;
private String Lat, Long;
//private String Id;
Double lat;
Double lng;
protected void onCreate(Bundle savedlnstanceSrate) {
super.onCreate(savedlnstanceSrate);
setContentView(R.layout.activity_map);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapB)).getMap();
mLocationClient = new LocationClient(this, this, this);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setRotateGesturesEnabled(true);
}
protected void onStart() {
mLocationClient.connect();
super.onStart();
}
protected void onStop() {
mLocationClient.disconnect();
super.onStop();
}
public void onConnectionFailed(ConnectionResult result) {
}
public void onConnected(Bundle connectionHint) {
if(mLocationClient.isConnected()){
mCurrentLocation = mLocationClient.getLastLocation();
coordinate = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 15));
mMarker = mMap.addMarker(new MarkerOptions().position(coordinate).icon(BitmapDescriptorFactory
.fromResource(R.drawable.pin4))
.draggable(true));
}
}
public void onDisconnected() {
}
public void onClick(View v) {
}
public void showMarker() {
String url = "http://103.7.56.128/~patsakorn/select_host.php";
Log.i("oooooo", "success");
try {
JSONArray data = new JSONArray(getJSONUrl(url));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("id_host", c.getString("id_host"));
map.put("lat", c.getString("lat"));
map.put("lng", c.getString("lng"));
map.put("name_host", c.getString("name_host"));
//map.put("image", c.getString("image"));
//map.put("information", c.getString("information"));
//map.put("imageinfo", c.getString("imageinfo"));
MyArrList.add(map);
Lat = map.get("lat");
Long = map.get("lng");
lat = Double.parseDouble(Lat);
lng = Double.parseDouble(Long);
mMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng)));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
Tag : Mobile, MySQL, Android, JAVA, Mobile
|
|
|
|
|
|
Date :
2014-06-17 14:40:44 |
By :
nickiestar |
View :
1377 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|