|
|
|
Android - ต้องการดึงค่าพิกัดละติจูด ลองจิจูด มาสร้างเป็น marker ใน google map แต่ไม่ได้ค่ะ |
|
|
|
|
|
|
|
Code (Android-Java)
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 com.project.arjj.R;
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 Map extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, OnClickListener {
GoogleMap mMap;
Marker mMarker;
Polyline direction;
LatLng coordinate;
LocationClient mLocationClient;
Location mCurrentLocation;
private String Id,name, Lat, Long;
Double lat;
Double lng;
protected void onCreate(Bundle savedlnstanceSrate) {
super.onCreate(savedlnstanceSrate);
setContentView(R.layout.mainthree);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).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, 18));
mMarker = mMap.addMarker(new MarkerOptions().position(coordinate).icon(BitmapDescriptorFactory
.fromResource(R.drawable.bubble))
.draggable(true));
}
}
public void onDisconnected() {
}
public void onClick(View v) {
}
public void showMarker() {
String url = "http://teawnai.com/travel2/rattana/testrattana.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", c.getString("Id"));
map.put("Lat", c.getString("Lat"));
map.put("Long", c.getString("Long"));
map.put("name", c.getString("name"));
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("Long");
lat = Double.parseDouble(Lat);
lng = Double.parseDouble(Long);
mMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.bubble)));
}
} 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();
}
}
มันไม่ยอมดึงค่ามาสร้างเป็นตัว marker บนแผนที่ให้ค่ะ ไม่รู้ผิดตรงไหน ผู้รู้ช่วยทีนะค่ะ
Tag : Mobile, Android, JAVA
|
|
|
|
|
|
Date :
2014-05-07 22:54:49 |
By :
sanay |
View :
2010 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Android-Java)
public void showMarker() {
String url = "http://teawnai.com/travel2/rattana/testrattana.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", c.getString("Id"));
map.put("Lat", c.getString("Lat"));
map.put("Long", c.getString("Long"));
map.put("name", c.getString("name"));
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("Long");
lat = Double.parseDouble(Lat);
lng = Double.parseDouble(Long);
mMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.bubble)));
}
ตอนนี้พวกนี้มันแสดงข้อมูลหรือโหลดจากเว็บได้แล้วหรือยังครับ ลองใช้พวก Log ทำการ Write ค่าดู หรือ Debug ดูได้หรือไม่ ??
|
|
|
|
|
Date :
2014-05-08 09:30:33 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองศึกษาเพิ่มเติมบทความ Android กับ Google Map ครับ
Android Google Map (Step by Step)
|
|
|
|
|
Date :
2015-11-18 14:15:31 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|