【问题标题】:how can i retrieve user location and show him the map我如何检索用户位置并向他展示地图
【发布时间】:2012-03-22 08:19:23
【问题描述】:

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

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

注意:我在 2.1 中的应用程序应该使用 google API 吗?如果是,Google 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();
                }

        }




}

这是另一个代码,这是使用 google API,但我在使用此代码时有 2 个问题: 1-它不检索用户位置,它只是给我看地图。 2-谷歌 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 成员你是我的英雄!请帮帮我!

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    您可以使用谷歌地图。 您将需要 Map Api 密钥。 如果您正在使用 Eclipse 进行编码,那么您可以按照链接上给出的过程进行操作 http://mobiforge.com/developing/story/using-google-maps-android

    【讨论】:

    • 我正在更新我的问题,我已经在做谷歌 API 代码,但你能回答我的问题吗?感谢快速重播,等待
    • 扩展 MapActivity 实现 LocationListener
    • 使用LocationListener类获取用户所在位置的经纬度
    • public void onLocationChanged(Location location) { // TODO 自动生成的方法存根 if (location != null) { lat = location.getLatitude(); lng = location.getLongitude(); String currentLocation = "位置改为Lat:" + lat + " Lng:" + lng; //myLoc.setText(currentLocation);
    • 在创建新项目时,您可以选择“Google 地图”作为目标,并且与地图一起,您还可以使用它进行所有其他操作。是的,谷歌地图也适用于其他活动。
    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    相关资源
    最近更新 更多