【问题标题】:Android: How do I display users current location in my app, using google maps?Android:如何使用谷歌地图在我的应用中显示用户当前位置?
【发布时间】:2011-10-09 12:58:27
【问题描述】:

如何使用谷歌地图在我的应用中显示用户当前位置?我想使用移动网络或 GPS(如果可用)向用户显示当前位置。我没有找到任何可以帮助我的教程,或者它们太难理解了。

这是我的 Map.java 活动的代码,你能给我我需要的代码吗?谢谢!

import java.util.List;

public class Map extends MapActivity
{

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.map);

 MapView mapView = (MapView) findViewById(R.id.mapview);
 mapView.setBuiltInZoomControls(true);

 MapController mc = mapView.getController();
 String coordinates[] = {"54.6738310", "025.2740480"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
     (int) (lat * 1E6), 
     (int) (lng * 1E6));

 mc.animateTo(p);
 mc.setZoom(13); 
 mapView.invalidate();

 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
 GeoPoint point = new GeoPoint(30443769,-91158458);
 OverlayItem overlayitem = new OverlayItem(point, "Laissez les bon temps rouler!", "I'm in Louisiana!");

 GeoPoint point2 = new GeoPoint(17385812,78480667);
 OverlayItem overlayitem2 = new OverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!");

 itemizedoverlay.addOverlay(overlayitem);
 itemizedoverlay.addOverlay(overlayitem2);

 mapOverlays.add(itemizedoverlay);
 }


 @Override
 protected boolean isRouteDisplayed()
 {
 return false;
 }
}

【问题讨论】:

    标签: android maps location position


    【解决方案1】:

    您需要将地图视图与位置管理器结合使用。多亏了这一点,您可以获取用户位置并将其绘制在地图中(作为另一个叠加层)。看看这个link,这是一个获取职位的教程。

    【讨论】:

    • 我是 android 和 java 新手,所以很难理解这些教程。也许更具体的东西?喜欢 - 分步教程?
    • @Simonas:你试过那个教程吗?是不是更具体..?
    • 该教程只涉及获取坐标,但没有关于 Google 地图的内容(是的,我是新人 :D)。
    • 使用 LocationManager 获取用户位置,获取一个 Location 对象,您将在其中获得坐标(纬度和经度)。这样,创建一个新的地理点,使用该点创建一个新的 itemizedOverlay 并将其添加到地图中。有时我们都是新人,但最好的学习方式是练习。我真的建议您完成本教程(或其他类似教程),了解其工作原理,然后将其与您的地图结合使用。也看看这个线程:stackoverflow.com/questions/3109158/…
    【解决方案2】:
        public class MainActivity extends FragmentActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        GoogleMap.OnMarkerDragListener,
        GoogleMap.OnMapLongClickListener,
        GoogleMap.OnMarkerClickListener, LocationListener,
        View.OnClickListener {
    
    
    private static final String TAG = "MapsActivity";
    private GoogleMap mMap;
    private GoogleApiClient googleApiClient;
    
    private Marker mCurrLocationMarker;
    private LocationRequest mLocationRequest;
    private ArrayList<LatLng> routePoints;
    private Polyline line;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
    
        //Initializing googleApiClient
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    
        routePoints = new ArrayList<LatLng>();
    }
    
    @Override
    public void onClick(View v) {
    
    }
    
    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setSmallestDisplacement(0.1F); //added
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //changed
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, this);
    }
    
    
    
    
      @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        // googleMapOptions.mapType(googleMap.MAP_TYPE_HYBRID)
        //    .compassEnabled(true);
    
        // Add a marker in Sydney and move the camera
        LatLng india = new LatLng(20.5937, 78.9629);
        mMap.addMarker(new MarkerOptions().position(india).title("Marker in India"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(india));
        mMap.setOnMarkerDragListener(this);
        mMap.setOnMapLongClickListener(this);
    }
    
    
    @Override
    protected void onStart() {
        googleApiClient.connect();
        super.onStart();
    }
    
    @Override
    protected void onStop() {
        googleApiClient.disconnect();
        super.onStop();
    }
    
    @Override
    public void onLocationChanged(Location location) {
    
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
    
    
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        mCurrLocationMarker = mMap.addMarker(markerOptions);
    
        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
    
        PolylineOptions pOptions = new PolylineOptions()
                .width(5)
                .color(Color.GREEN)
                .geodesic(true);
        for (int z = 0; z < routePoints.size(); z++) {
            LatLng point = routePoints.get(z);
            pOptions.add(point);
        }
        line = mMap.addPolyline(pOptions);
        routePoints.add(latLng);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多