|
|
|
สอบถามหน่อยคะ สร้างแอพแอนดรอยนำทางไปยังพิกัดที่ต้องการ โดยดึงพิกัดมาจาก Appserv |
|
|
|
|
|
|
|
มัน error อ่าคะ ช่วยดูให้หน่อยคะ
โค๊ด MapsActivity.java
package app.meter.com.meter;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class MapsActivity extends FragmentActivity {
// Google Map
private GoogleMap googleMap;
// Latitude & Longitude
private Double LAT = 0.00;
private Double LONG = 0.00;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//*** Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ArrayList<HashMap<String, String>> location = null;
String url = "http://localhost/getLatLon.php";
try {
JSONArray data = new JSONArray(getHttpGet(url));
location = 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("FID", c.getString("FID"));
map.put("LAT", c.getString("LAT"));
map.put("LONG", c.getString("LONG"));
map.put("PEANO", c.getString("PEANO"));
location.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// *** Display Google Map
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();
// *** Focus & Zoom
LAT = Double.parseDouble(location.get(0).get("LAT").toString());
LONG = Double.parseDouble(location.get(0).get("LONG").toString());
LatLng coordinate = new LatLng(LAT, LONG);
googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));
// *** Marker (Loop)
for (int i = 0; i < location.size(); i++) {
LAT = Double.parseDouble(location.get(i).get("LAT").toString());
LONG = Double.parseDouble(location.get(i).get("LONG").toString());
String name = location.get(i).get("PEANO").toString();
MarkerOptions marker = new MarkerOptions().position(new LatLng(LAT, LONG)).title(name);
googleMap.addMarker(marker);
}
}
public static String getHttpGet(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 result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
โค๊ด activity_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/googleMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Tag : Mobile, MySQL, JavaScript, Device (Mobile), Android, Mobile
|
ประวัติการแก้ไข 2016-04-18 16:51:31 2016-04-18 22:45:23
|
|
|
|
|
Date :
2016-04-18 16:43:48 |
By :
aummyasia |
View :
1560 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูตัวอย่างนี้ครับ
|
|
|
|
|
Date :
2016-04-18 16:50:13 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองกด Clean Project แล้ว Build Project ใหม่ครับ
|
|
|
|
|
Date :
2016-04-30 16:51:54 |
By :
cre_kiwsan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-05-02 22:48:56 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|