Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android รบกวนช่วยดู Code เกี่ยวกับ Android Google Map API V2 MySQL



 

Android รบกวนช่วยดู Code เกี่ยวกับ Android Google Map API V2 MySQL

 



Topic : 089859

Guest




ผมต้องการ add marker บน map โดยดึงข้อมูลจาก database
ตอนนี้ Code ไม่มี Error แต่มีปัญหาตอน Run หรือ Debug
เลยคิดว่าน่าจะมีตรงไหนที่ผิด เลยอยากจะรบกวนผู้เชี่ยวชาญ
ช่วยชี้แนะส่วนที่ผิดพลาด และแนวทางการแก้ไขครับ ขอบคุณครับ

Code (Android-Java)
package cpc.shopbedocafe;

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.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
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 android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;

public class MapActivity extends FragmentActivity implements OnInfoWindowClickListener {
	private GoogleMap myMap;
	private Double lat,lng;
	private LocationManager locationManager;
	private String userid;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		showMarker();
		
		FragmentManager myFragmentManager = getSupportFragmentManager();
		SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map_canvas);
		myMap = mySupportMapFragment.getMap();
		
		myMap.setOnInfoWindowClickListener(this);
		myMap.setMyLocationEnabled(true);
		
		myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lng), 8));
		
		LocationListener locationListener = new LocationListener(){
        	
		      
			public void onLocationChanged(Location location) {		        

				lat = location.getLatitude();
				lng = location.getLongitude();
		        
		        myMap.addMarker(new MarkerOptions()
		        	.position(new LatLng(lat,lng))
		        	.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
		        	.title("You Here"));
		    }

			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}

			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub
				
			}
        };
        
        
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}

	public void showMarker() {
		new LoadContentFromServer().execute();
	}
	
	class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {
		
		@Override
		protected Object doInBackground(Object... params) {
		
		String url = "http://s.creation-it.com/php/showallshop.php";
		
		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("UserID", c.getString("UserID"));
				map.put("Name", c.getString("Name"));
				map.put("Address", c.getString("Address"));
				map.put("Latitude", c.getString("Latitude"));
				map.put("Longitude", c.getString("Longitude"));
				map.put("Picture", c.getString("Picture"));
				MyArrList.add(map);
				
				userid = map.get("UserID");
				String name = map.get("Latitude");
				String address = map.get("Address");
				String latitude = map.get("Latitude");
				String longitude = map.get("Longitude");
				
				myMap.addMarker(new MarkerOptions()
				.position(new LatLng(Double.parseDouble(latitude),Double.parseDouble(longitude)))
				.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
				.title(name)
				.snippet(address));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
	
	@Override
	public void onInfoWindowClick(Marker marker) {
		
		marker.hideInfoWindow();
		Intent it = new Intent(MapActivity.this,ShopProfileActivity.class);
		it.putExtra("UserID", userid);
		startActivity(it);
	}
	
	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) {
				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();
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map, menu);
		return true;
	}

}





Tag : Mobile, MySQL, Android







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-01-27 16:55:34 By : Lay View : 3247 Reply : 8
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ใช้การ Debug ดูค่านี้ครับ getJSONUrl(url)






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-01-28 20:20:43 By : mr.win
 


 

No. 2

Guest


ขอบคุณครับ ให้รุ่นพี่ช่วยดู Code เจอปัญหาแล้วว่าไม่มี onPostExecute ครับ กับการวางตำแน่งของคำสั่งต่าง ๆ นิดหน่อย ซึ่งแก้ไขแล้วสามารถใช้งานได้ตามปกติแล้วครับ

Code (Android-Java)
package cpc.shopbedocafe;

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.GooglePlayServicesUtil;
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.GoogleMap.OnInfoWindowClickListener;
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 android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MapActivity extends FragmentActivity implements OnInfoWindowClickListener {

	final int RQS_GooglePlayServices = 1;
	private GoogleMap myMap;
	private LocationManager locationManager;
	private String userid;
	private String name, address, latitude, longitude;
	Double lat;
	Double lng;

	final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
	HashMap<String, String> map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		FragmentManager myFragmentManager = getSupportFragmentManager();
		SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
				.findFragmentById(R.id.map_canvas);
		myMap = mySupportMapFragment.getMap();
		
		myMap.setOnInfoWindowClickListener(this);
		myMap.setMyLocationEnabled(true);

		LocationListener locationListener = new LocationListener() {

			public void onLocationChanged(Location location) {
				LatLng latlng = new LatLng(location.getLatitude(),
						location.getLongitude());
				myMap.animateCamera(CameraUpdateFactory.newLatLngZoom((latlng),
						10));
			}

			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub

			}

			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub

			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub

			}
		};

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 20, 20, locationListener);

		showMarker();
	}

	@Override
	protected void onResume() {
		super.onResume();

		int resultCode = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(getApplicationContext());

		if (resultCode == ConnectionResult.SUCCESS) {
			Toast.makeText(getApplicationContext(),
					"GooglePlayServices is Available", Toast.LENGTH_LONG)
					.show();
		} else {
			GooglePlayServicesUtil.getErrorDialog(resultCode, this,
					RQS_GooglePlayServices);
		}
	}
	
	@Override
	public void onInfoWindowClick(Marker marker) {
		
		marker.hideInfoWindow();
		Intent it = new Intent(MapActivity.this,ShopProfileActivity.class);
		it.putExtra("UserID", userid);
		startActivity(it);
	}

	public void addMarker() {
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map, menu);
		return true;
	}

	public void showMarker() {
		new LoadContentFromServer().execute();
	}

	class LoadContentFromServer extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {

			String url = "http://s.creation-it.com/php/showallshop.php";
			return getJSONUrl(url);
		}

		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) {
					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();
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				JSONArray data = new JSONArray(result);

				for (int i = 0; i < data.length(); i++) {
					JSONObject c = data.getJSONObject(i);

					map = new HashMap<String, String>();
					map.put("UserID", c.getString("UserID"));
					map.put("Name", c.getString("Name"));
					map.put("Address", c.getString("Address"));
					map.put("Latitude", c.getString("Latitude"));
					map.put("Longitude", c.getString("Longitude"));
					map.put("Picture", c.getString("Picture"));
					MyArrList.add(map);

					userid = map.get("UserID");
					name = map.get("Name");
					address = map.get("Address");
					latitude = map.get("Latitude");
					longitude = map.get("Longitude");
					lat = Double.parseDouble(latitude);
					lng = Double.parseDouble(longitude);

					try {
						myMap.addMarker(new MarkerOptions()
							.position(new LatLng(lat, lng))
							.icon(BitmapDescriptorFactory
							.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
							.title(name).snippet(address));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-01-30 02:29:08 By : Two
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

จัดไปครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-01-30 05:49:22 By : mr.win
 


 

No. 4

Guest


สวัสดีครับพี่ ผมอยากทราบว่าพี่พอจะทำบทความเพื่อเผยแพร่ให้ได้รึเปล่าครับ ในเรื่องนี้อ่ะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-26 10:27:44 By : สมพงษ์
 


 

No. 5



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ตอบความคิดเห็นที่ : 4 เขียนโดย : สมพงษ์ เมื่อวันที่ 2013-12-26 10:27:44
รายละเอียดของการตอบ ::
มันมีอยู่ในบทความครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-26 10:39:57 By : mr.win
 


 

No. 6

Guest


ไม่ทราบว่าคุณพอจะจำลิงก์ได้รึเปล่าครับ ผมเข้าไปดูมีแต่ของ php ไม่เห็นมีของ android เลยครับ รบกวนช่วยหาหน่อยได้ไหมครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-27 13:44:54 By : สมพงษ์
 


 

No. 7



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ลองศึกษาเพิ่มเติมบทความ Android กับ Google Map ครับ



Android Google Map (Step by Step)

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-11-18 14:13:02 By : mr.win
 


 

No. 8



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์


จากบล็อกของ admin เขียนมาจาก https://www.thaicreate.com/mobile/android-google-map-marker-location.html ผมได้ลองทำดูแล้วมันแดงที่ getmap(); อ่ะครับ ไม่ทราบว่าเป็นเพราะอะไรครับ จากโค้ดด้านล่างที่ตัวอักษรสีแดงครับ ขอบคุณครับ

Code
package com.myapp; import android.os.Bundle; import android.support.v4.app.FragmentActivity; 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.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; public class MainActivity extends FragmentActivity implements LocationListener { protected LocationManager locationManager; protected LocationListener locationListener; // Google Map private GoogleMap googleMap; // Latitude & Longitude private Double Latitude = 0.00; private Double Longitude = 0.00; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //*** Location locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); //*** Display Google Map googleMap = ((SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.googleMap)).getMap(); } @Override public void onLocationChanged(Location location) { if (location == null) { return; } //** Get Latitude & Longitude Latitude = location.getLatitude(); Longitude= location.getLongitude(); googleMap.clear(); //*** Focus & Zoom LatLng coordinate = new LatLng(Latitude, Longitude); googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17)); //*** Marker MarkerOptions marker = new MarkerOptions() .position(new LatLng(Latitude, Longitude)).title("Your current location"); marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)); googleMap.addMarker(marker); } @Override public void onProviderDisabled(String provider) { // Log.d("Latitude","disable"); } @Override public void onProviderEnabled(String provider) { // Log.d("Latitude","enable"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // Log.d("Latitude","status"); } }



ประวัติการแก้ไข
2016-10-13 12:42:35
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-10-13 12:39:53 By : arthit
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android รบกวนช่วยดู Code เกี่ยวกับ Android Google Map API V2 MySQL
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่