Android Google Maps将标记位置保存到MySQL数据库
我对安卓和一般编程都很陌生。我正在开发一个Android应用程序,允许用户在Google地图上放置一个标记,一旦放置了标记,其位置(纬度和经度)将上传到存储在服务器上的外部MySQL数据库 为了让我的谷歌地图在Android上正常工作,我遵循了以下系列教程: 现在我想能够上传纬度和经度到我的数据库,我不知道怎么做 这是我现在拥有的代码:Android Google Maps将标记位置保存到MySQL数据库,android,mysql,google-maps,google-maps-markers,android-mapview,Android,Mysql,Google Maps,Google Maps Markers,Android Mapview,我对安卓和一般编程都很陌生。我正在开发一个Android应用程序,允许用户在Google地图上放置一个标记,一旦放置了标记,其位置(纬度和经度)将上传到存储在服务器上的外部MySQL数据库 为了让我的谷歌地图在Android上正常工作,我遵循了以下系列教程: 现在我想能够上传纬度和经度到我的数据库,我不知道怎么做 这是我现在拥有的代码: public class Main extends MapActivity implements LocationListener{ /** Called w
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();
}
});
我非常感谢您的帮助,因为我不知道如何将标记的位置发送到SaveRemote方法,而SaveRemote方法又会将这些数据发送到php文件
谢谢你嘿,我可能有点晚了,但是你可以使用setonmarkerdragstener解决这个问题,比如:
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
//Toast.makeText(TerminosYC.this.getActivity(), "Arrastra el marcador, hasta el lugar deseado",
// Toast.LENGTH_SHORT).show();
LatLng position = marker.getPosition();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(position.latitude, position.longitude), 15));
}
@Override
public void onMarkerDrag(Marker marker) {
// System.out.println("Arrastrando...");
LatLng position = marker.getPosition();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(position.latitude, position.longitude), 15));
}
@Override
public void onMarkerDragEnd(Marker marker) {
LatLng position = marker.getPosition();
latitudeeeee = position.latitude;
longitudeeee = position.longitude;
//Toast.makeText(
// TerminosYC.this.getActivity(),
// "Lat " + position.latitude + " "
// + "Long " + position.longitude,
// Toast.LENGTH_LONG).show();
//map.animateCamera(CameraUpdateFactory.newLatLngZoom(
// new LatLng(position.latitude, position.longitude), 15));
}
});
}
});
这是一篇很老的帖子——你是已经“自助”了,还是仍然停留在那里?基本上,方法是从您的地质点提取坐标,然后使用REST/Json提交它们。我非常感谢您,因为您在5年后就回答了这个问题。欢迎您,您的努力是值得的。那很酷。
$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);
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
//Toast.makeText(TerminosYC.this.getActivity(), "Arrastra el marcador, hasta el lugar deseado",
// Toast.LENGTH_SHORT).show();
LatLng position = marker.getPosition();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(position.latitude, position.longitude), 15));
}
@Override
public void onMarkerDrag(Marker marker) {
// System.out.println("Arrastrando...");
LatLng position = marker.getPosition();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(position.latitude, position.longitude), 15));
}
@Override
public void onMarkerDragEnd(Marker marker) {
LatLng position = marker.getPosition();
latitudeeeee = position.latitude;
longitudeeee = position.longitude;
//Toast.makeText(
// TerminosYC.this.getActivity(),
// "Lat " + position.latitude + " "
// + "Long " + position.longitude,
// Toast.LENGTH_LONG).show();
//map.animateCamera(CameraUpdateFactory.newLatLngZoom(
// new LatLng(position.latitude, position.longitude), 15));
}
});
}
});