Android อยากสอบถามเกี่ยวกับการส่งค่า intent เพื่อสร้าง Google map
อยากทราบวิธีการที่จะส่งค่า ชื่อ ละติจูด ลองติจูด ที่รับมาจาก Localhost มาสร้าง Map ใน Google เพื่อมาร์คจุดอ่ะครับ จะสามารถทำได้ยังไง ผมพยายามใช้ Code นี้ รับค่ามาได้ แต่จะส่งไปเพื่อให้ Google map แสดง ก็ไม่สามารถรับได้ และกดปุ่มให้เด้ง google map แต่มันก็เด้งออก
หน้า AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crmtracking.testokhttp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MapActivity"/>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
หน้า MainActivity
Code (Android-Java)
package com.example.crmtracking.testokhttp;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
OkHttpClient okHttpClient = new OkHttpClient();
private ImageView imageView;
private TextView textView;
private String nameString, latString, lngString;
private String[] latStrings, lngStrings;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
textView = (TextView) findViewById(R.id.textView);
AsyncTaskGetData taskGetData = new AsyncTaskGetData();
taskGetData.execute("cd298861-4cd3-1e93-2cec-5757f20ee3b5");
AsyncTaskGetImage taskGetImage = new AsyncTaskGetImage();
taskGetImage.execute();
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
public class AsyncTaskGetData extends AsyncTask<String , Void, Void> {
private String x;
@Override
protected void onPostExecute(Void aVoid) {
try {
JSONArray jsonArray = new JSONArray(x);
final String[] nameStrings = new String[jsonArray.length()];
latStrings = new String[jsonArray.length()];
lngStrings = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
nameStrings[i] = jsonObject.getString("name");
latStrings[i] = jsonObject.getString("jjwg_maps_lat");
lngStrings[i] = jsonObject.getString("jjwg_maps_lng");
} // for
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putExtra("name", nameStrings[i]);
intent.putExtra("jjwg_maps_lat", latStrings[i]);
intent.putExtra("jjwg_maps_lng", lngStrings[i]);
startActivity(intent);
} // onItem
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(String... params) {
RequestBody body = new FormEncodingBuilder()
.add("sID", params[0])
.build();
Request request = new Request.Builder()
.url("http://161.246.58.129/tablemap/table/jsonmap.php")
.post(body)
.build();
Response response;
try {
response = okHttpClient.newCall(request).execute();
String result = response.body().string();
JSONObject object;
try {
object = new JSONObject(result);
String id = object.getString("id");
String name = object.getString("name");
String description = object.getString("description");
String jjwg_maps_lat = object.getString("jjwg_maps_lat");
String jjwg_maps_lng = object.getString("jjwg_maps_lng");
x = "ID : " + id + "\n" + "ชื่อ : " + name + "\n" + "ที่อยู่ : " + description + "\n" + "Latitude : " + jjwg_maps_lat
+ "\n" + "Longitude : " + jjwg_maps_lng;
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public class AsyncTaskGetImage extends AsyncTask<String, Void, Void> {
private Bitmap bitmap;
@Override
protected void onPostExecute(Void aVoid) {
imageView.setImageBitmap(bitmap);
}
@Override
protected Void doInBackground(String... params) {
Request request = new Request.Builder()
.url("https://pbs.twimg.com/profile_images/606585229034135553/2NqZJYQI.png")
.build();
Response response;
try {
response = okHttpClient.newCall(request).execute();
InputStream result = response.body().byteStream();
bitmap = BitmapFactory.decodeStream(result);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
หน้า MapActivity
Code (Android-Java)
package com.example.crmtracking.testokhttp;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String nameStrings, latString, lngString;
ArrayList<Geofence> mGeofences;
private GeofenceStore mGeofenceStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean isGPS_Enabled = false;
boolean isNetwork_Enabled = false;
try {
isGPS_Enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch (Exception ex) { }
try {
isNetwork_Enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch (Exception ex) { }
if (!isGPS_Enabled && !isNetwork_Enabled) {
adb.setTitle("Warning Location Services!!");
adb.setMessage("Please Enable Location Services.");
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
});
adb.show();
}
mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
//Receive From Intent
nameStrings = getIntent().getStringExtra("name");
latString = getIntent().getStringExtra("jjwg_maps_lat");
lngString = getIntent().getStringExtra("jjwg_maps_lng");
//Show View
TextView textView = (TextView) findViewById(R.id.textView10);
textView.setText(nameStrings);
} // Main Method
public void clickBackPlate(View view) {
finish();
}
@Override
public void onMapReady(GoogleMap googleMap) {
} // onMap
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
setUpMapIfNeeded();
} else {
GooglePlayServicesUtil.getErrorDialog(
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
this, 0);
}
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
UiSettings uis = mMap.getUiSettings();
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.729468, 100.779361), 16));
mMap.setIndoorEnabled(false);
uis.setCompassEnabled(true);
}
}
Tag : Mobile, Device (Mobile), Android, JAVA, Mobile
Date :
2016-06-15 16:33:53
By :
ithikom5004
View :
1265
Reply :
1
ลอง Debug ดู LogCat ครับ
Date :
2016-06-15 17:03:40
By :
mr.win
Load balance : Server 03