0

我對Android和編程一般都很陌生。我正在開發一個Android應用程序,讓用戶在Google地圖上放置一個標記,一旦放置標記,它的位置(經度和緯度)就會上傳到存儲在服務器上的外部MySQL數據庫。Android Google Maps將標記位置保存到MySQL數據庫

我跟着這些教程系列讓我的谷歌地圖在Android上正常工作:

Google Maps Tutorials

現在我希望能夠上傳緯度和經度到我的數據庫,我不知道如何做到這一點。

這是代碼,我現在所擁有的:

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」,您可以看到我編寫的代碼在用戶按下的位置放置了一個標記。在Button 1代碼的最後,我調用SaveRemote方法,該方法負責根據放置標記的位置保存實際的經度和緯度。

我不知道該怎麼把這個方法,以及如何將它連接到我的主程序,並從用戶按下的地方得到實際的經度和緯度。

這是我寫

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查詢它增加了標記的緯度和經度到我的服務器上的insert.php文件的方法的代碼數據庫。

這是實際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文件。

謝謝

+0

這是一個很舊的文章 - 你已經「幫助自己」或者是你仍然在那裏?基本上,要走的路是從GeoPoint中提取座標,然後使用REST/Json提交座標。 – richey

回答

0

嘿,我可能是有點晚了,但你可以解決這個使用setOnMarkerDragListener是這樣的:

  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)); 

       } 
      }); 

     } 
    }); 
+1

你對我的感謝,因爲你在5年後回答了這個問題。 – 2017-06-05 01:43:18

+0

@aendeerei heeey感謝夥計! –

+1

不客氣,你真的值得你的努力。這很酷。 – 2017-06-05 01:46:46

相關問題