我如何检索用户位置并向他显示地图

时间:2012-03-04 16:54:07

标签: java android google-maps

我有疑问,我真的需要一些帮助: 我开发应用程序,我想:

1 - 检索用户位置并在地图中显示。 2-用户可以将引脚放在地图中的任何位置,我应该协调这个引脚并将其存储在变量中

注意:我在2.1中的应用程序应该使用google API吗?如果是,谷歌API是否支持任何活动而不是地图?

我在谷歌搜索并找到此代码:

package com.java.locate;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;



  public class AndroidLbsGeocodingProjectActivity extends Activity {
   /** Called when the activity is first created. */
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
    protected LocationManager locationManager;
    protected Button retrieveLocationButton;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                   new MyLocationListener()
                );

            retrieveLocationButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        showCurrentLocation();
                    }
            });       

            }   

            protected void showCurrentLocation() {

                Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                if (location != null) {
                    String message = String.format(
                            "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                            location.getLongitude(), location.getLatitude()
                    );
                    Toast.makeText(AndroidLbsGeocodingProjectActivity.this, message,
                            Toast.LENGTH_LONG).show();
                }

            }  

            private class MyLocationListener implements LocationListener {

                public void onLocationChanged(Location location) {
                    String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                            location.getLongitude(), location.getLatitude()
                    );
                    Toast.makeText(AndroidLbsGeocodingProjectActivity.this, message, Toast.LENGTH_LONG).show();
                }

                public void onStatusChanged(String s, int i, Bundle b) {
                    Toast.makeText(AndroidLbsGeocodingProjectActivity.this, "Provider status changed",
                            Toast.LENGTH_LONG).show();
                }

            public void onProviderDisabled(String s) {
                    Toast.makeText(AndroidLbsGeocodingProjectActivity.this,
                            "Provider disabled by the user. GPS turned off",
                            Toast.LENGTH_LONG).show();
                }

                public void onProviderEnabled(String s) {
                    Toast.makeText(AndroidLbsGeocodingProjectActivity.this,
                            "Provider enabled by the user. GPS turned on",
                            Toast.LENGTH_LONG).show();
                }

        }




}

这是另一个代码,这是通过使用谷歌API,但我有2个问题使用此代码: 1 - 它不检索用户位置,它只是显示我的地图。 2-google API是否与其他活动合作而不仅仅是地图?

  package our.google.maps;

  import java.util.List;
  import java.util.Locale;

  import com.google.android.maps.GeoPoint;
  import com.google.android.maps.MapActivity;
  import com.google.android.maps.MapController;
  import com.google.android.maps.MapView;
  import com.google.android.maps.MyLocationOverlay;
  import com.google.android.maps.Overlay;

  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.location.Geocoder;
  import android.location.Location;
  import android.location.LocationListener;
  import android.os.Bundle;
  import android.view.MotionEvent;
  import android.widget.Toast;

  public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */
long start;
long stop;
MyLocationOverlay compass;
MapController controller;
MapView map;
  //136
int x,y;
GeoPoint touchedPoint;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    map=(MapView) findViewById(R.id.mvMain);
    map.setBuiltInZoomControls(true);
    Touchy t = new Touchy();
    List<Overlay> overlaylist= map.getOverlays();
    overlaylist.add(t);
    compass = new MyLocationOverlay (MapsActivity.this, map);
    overlaylist.add(compass);
    //map controller to go to Specific Location n36eeh el 6ol & el 3r'9  135

    controller=map.getController();
    GeoPoint point = new GeoPoint (51643234 , 7848593);
    controller.animateTo(point);
    controller.setZoom(6);

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    compass.disableCompass();

    super.onPause();
  }
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    compass.enableCompass();
    super.onResume();
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
class Touchy extends Overlay {
    public boolean onTouchEvent(MotionEvent e,MapView m){
        if(e.getAction() == MotionEvent.ACTION_DOWN){
            start = e.getEventTime();
            x = (int) e.getX();
            y = (int) e.getY();
            touchedPoint = map.getProjection().fromPixels(x, y);

        }
        if (e.getAction() == MotionEvent.ACTION_UP) {
            stop =e.getEventTime();
        }
        if(stop - start >1500) {
            AlertDialog alert = new AlertDialog.Builder(MapsActivity.this).create();
        alert.setTitle("Pick an option");
        alert.setMessage(" i told u to pick an option"); 
        alert.setButton("place a pin", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        } );

     alert.setButton2("get an address", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Geocoder geocode = new Geocoder   (getBaseContext(),Locale.getDefault());
        try {

        }
        finally{}}}
         );

      alert.setButton3("option3 ", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

}
    } );
     alert.show();
      return true;
        }
    return false;   
    }
}
private class MyLocationListener implements LocationListener{


    public void onLocationChanged1(Location location) {
        String message = String.format(
            "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MapsActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(MapsActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

public void onProviderDisabled(String s) {
        Toast.makeText(MapsActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(MapsActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

 }

 }

另外,如果我的代码不正确,你能告诉我正确的男女同校吗? StackOverFlow成员你是我的英雄!帮帮我!

1 个答案:

答案 0 :(得分:0)

您可以使用Google地图。 您将需要Map Api键。 如果您正在编写使用Eclipse,那么您可以按照链接上给出的步骤进行操作 http://mobiforge.com/developing/story/using-google-maps-android