저는 Android 및 프로그래밍 전반에 새로운 경험이 있습니다. 사용자가 Google지도에 마커를 배치 할 수있는 Android 애플리케이션을 개발 중입니다. 마커가 배치되면 위치 (위도 및 경도)가 서버에 저장된 외부 MySQL 데이터베이스에 업로드됩니다. Android Google지도는 MySQL 데이터베이스에 마커 위치를 저장합니다.
은 내 Google지도 안드로이드에서 제대로 작동하도록이 튜토리얼 시리즈 다음 :지금 내 데이터베이스에 위도와 경도를 업로드 할 수 있도록하려면을 나는 방법을 잘 모르겠어요 해.
이
내가 지금이 그 코드 : 이public class Main extends MapActivity implements LocationListener{
/** Called when the activity is first created. */
//set variables
MapView map; //set map to mapview
long start; //create a starting point when the user presses down on the map
long stop; //create a stop point when the user stops pressing down on the map
MapController controller; //create a map controller that animates where the user is going through the map activity
int x, y; //x and y position on the overlay when the user touches the screen
GeoPoint touchedPoint; //create a geopoint as touchedPoint
Drawable d; //create drawable item
List<Overlay> overlayList; //setup list of overlays
LocationManager lm; //setup location manager
String towers; //setup string called towers
int lat; //create lattitude for the phone's current position
int longi; //create longitude for the phone's current position
private ArrayList<ToDoItem> todoItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView)findViewById(R.id.mvMain);//set map to mapview in the main.xml
map.setBuiltInZoomControls(true);//zoom function
Touchy t = new Touchy();//new instance of touchy class
overlayList = map.getOverlays();//list of overlays
overlayList.add(t);//add touchy instance (t) to a list of overlays
controller = map.getController();//setup controller
GeoPoint point = new GeoPoint(51643234, 7848593);//setup geopoint
controller.animateTo(point);//animate controller to that location
controller.setZoom(6);//set zoom level
//create drawable item and its location
d = getResources().getDrawable(R.drawable.yellow);
//Placing marker at location
//retrieve location manager for controlling the location updates
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();//setup criteria
towers = lm.getBestProvider(crit, false);//return the provider that best fits the given criteria
Location location = lm.getLastKnownLocation(towers);//access location through location manager and get the last known location called towers
if (location != null){
lat = (int) (location.getLatitude() *1E6);//get latitude
longi = (int) (location.getLongitude() *1E6);//get longitude
//add overlay to the list
GeoPoint ourLocation = new GeoPoint(lat, longi);//setup geopoint as ourLocation with lat and long
OverlayItem overlayItem = new OverlayItem(ourLocation, "Hello", "2nd String");//pass in outLocation to OverlayItem
CustomMarker custom = new CustomMarker(d, Main.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}else{
Toast.makeText(Main.this, "Couldn't find provider", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause(){
super.onPause();
//when pause is pressed remove location updates
lm.removeUpdates(this);
}
@Override
protected void onResume(){
super.onResume();
//when the resume is pressed request location updates
lm.requestLocationUpdates(towers, 500, 1, this);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
//overlay class that handles all touch events
class Touchy extends Overlay{
public boolean onTouchEvent(MotionEvent e, MapView m){ //onTouchEvent method which includes a motion even and a map view
if(e.getAction() == MotionEvent.ACTION_DOWN){ //when the user presses down on the overlay
start = e.getEventTime();//start point when the event happens
x = (int) e.getX();//x and y coordinates based on where the user touched the overlay
y = (int) e.getY();
touchedPoint = map.getProjection().fromPixels(x, y);//sets a geopoint based on where the user has touched on the screen. Relates the map activity to the user's actual touch on the overlay
}
if (e.getAction() == MotionEvent.ACTION_UP){ //when the user stops pressing down on the overlay
stop = e.getEventTime();//stop point when the event happens
}
if (stop - start > 1500){ //if the user presses on the overlay for more than 1.5 seconds
AlertDialog alert = new AlertDialog.Builder(Main.this).create();//setup alert dialog for the Main class
alert.setTitle("Pick an Option");//set the title for the alert
alert.setMessage("Please pick an option");//set the message for the alert
//BUTTON 1
//set a button for the alert. Refer to a dialogue interface for this OnClickListener
alert.setButton("Place a Marker", new DialogInterface.OnClickListener() {
//when the button is clicked
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
OverlayItem overlayItem = new OverlayItem(touchedPoint, "Hello", "2nd String");//setup overlay item and refer to touchedPoint which is geopoint
CustomMarker custom = new CustomMarker(d, Main.this);//setup a custom marker with a "d" in the Main class
custom.insertPinpoint(overlayItem);//add an overlayItem to the pinpoint array list
overlayList.add(custom);//add custom overlay to the overlay list
map.invalidate();//make MapView draw itself
SaveRemote();
}
});
이것은 내가 내 프로그램에있는 모든 코드가 아닙니다,하지만 난 내 문제와 관련이 당신을 도울 수있는 모든 것을 포함하는 시도 그것을 해결하십시오.
"버튼 1"까지 아래로 스크롤하면 내가 작성한 코드가 사용자가 누른 위치에 마커를 배치하는 것을 볼 수 있습니다. 버튼 1 코드의 끝에서 마커의 위치에 따라 실제 위도와 경도를 저장하는 SaveRemote 메서드를 호출합니다.
이 방법에 무엇을 넣어야할지, 내 프로그램에 연결하고 사용자가 누른 위치에서 실제 위도와 경도를 얻는 방법을 모르겠습니다.
이
는 내가에 마커의 위도와 경도를 추가 삽입 쿼리를 실행 내 서버에 insert.php 파일을 호출 볼 수 있듯이 내가public void SaveRemote(){
String result = "";
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myservername.com/insert.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
작성한 메서드에 대한 코드입니다 데이터 베이스.
그리고 이것은 실제 insert.php 코드 :
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);
$result = mysql_query("INSERT INTO markers (lat, lng) VALUES (lat, lng)";
mysql_close($con);
난 정말 내가 다시이 데이터를 보내드립니다 SaveRemote 방법에 마커의 위치를 전송하는 방법을 알고하지 않는 한 당신의 도움을 주셔서 감사합니다 것입니다 PHP 파일.
는
이 꽤 오래된 게시물입니다 - 않았다 당신이 이미 '자신을 도와'또는 당신이 아직도 거기 붙어 있니? 기본적으로 GeoPoint에서 좌표를 추출한 다음 REST/Json을 사용하여 좌표를 전송하는 것입니다. – richey