【问题标题】:how to put marker on map when location gets changed in android当位置在android中改变时如何在地图上放置标记
【发布时间】:2011-08-08 22:14:26
【问题描述】:
package com.android.gps;

import java.util.List;

import com.android.gps.hellogps.MyLocation.MapOverlay;
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.MapView.LayoutParams;
import com.google.android.maps.Overlay;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class hellogps extends MapActivity {
    /** Called when the activity is first created. */
    MapView mapView; 
    MapController mc;
    GeoPoint p;
    LocationManager locationManager;
    hellogps x;
    MapOverlay mapOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.linear);  
        View zoomView = mapView.getZoomControls(); 

        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

        mc = mapView.getController();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocation(); 


        locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 10f, mlocListener);





    }



    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
public class MyLocation implements LocationListener
{

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

        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);



        GeoPoint p = new GeoPoint(lat, lng);

         mc.setZoom(17);
            mc.animateTo(p);
          List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);        

            mapView.invalidate();


         mapView.invalidate();

    }
       class MapOverlay extends com.google.android.maps.Overlay
        {
           MapOverlay mapOverlay=new MapOverlay();

            @Override
            public boolean draw(Canvas canvas, MapView mapView, 
            boolean shadow, long when) 
            {
                super.draw(canvas, mapView, shadow);                   

                //---translate the GeoPoint to screen pixels---
                Point screenPts = new Point();
                mapView.getProjection().toPixels(p, screenPts);

                //---add the marker---
                Bitmap bmp = BitmapFactory.decodeResource(
                    getResources(), R.drawable.push_pin);            
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
                return true;
            }
        } 

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }}}

我是 android 新手,正在做我的大学项目 当我运行我的应用程序时它工作正常但是 当我通过 telnet 发送位置坐标时,会出现蓝屏并且那里没有标记 请帮助我

【问题讨论】:

    标签: android google-maps android-emulator


    【解决方案1】:

    如果您只是想在地图上显示用户的当前位置,使用内置的 MyLocationOverlay 类可能会更容易。

    在您的 onCreate() 中使用以下内容:

            MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
            mapView.getOverlays().add(myLocationOverlay);
            myLocationOverlay.enableCompass(); // if you want to display a compass also
            myLocationOverlay.enableMyLocation();
    

    这将向用户显示当前位置并在他们四处走动时进行更新。它不会跟踪用户去过的地方。 (我不确定这是您要完成的任务还是只是显示当前位置。)

    【讨论】:

      【解决方案2】:

      onLocationChanged 方法中的MyLocation 类中,您应该编写而不是GeoPoint p = new GeoPoint(lat, lon); 尝试p = new GeoPoint(lat, lon);。在您的代码中,您创建了名为p 的新变量(与全局变量相同),并且您的局部变量指向对象。你的局部变量p 没有指向任何东西(= null)。因为你在draw中使用了你的全局变量p并设置为null。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        • 2011-07-09
        • 2015-01-26
        相关资源
        最近更新 更多