如何把标记在地图上的位置时,在Android中得到改变 [英] how to put marker on map when location gets changed in android

查看:212
本文介绍了如何把标记在地图上的位置时,在Android中得到改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    }}}

我为新的机器人做我colege项目 当我运行我的应用程序,它工作正常,但  当我发送的位置坐标throgh的telnet它出现蓝屏,并没有markar有 请帮我

i m new in android doing my colege project When i run my app it works fine but when i send location coordinates throgh telnet it a blue screen appears and no markar there pls help me

推荐答案

在你的 MyLocation 类中的 onLocationChanged 方法你应该写,而不是的GeoPoint P =新的GeoPoint(纬度,经度); 尝试 P =新的GeoPoint(纬度,经度); 。在您的code创建 P 所谓的新变量(相同的全局变量)和局部变量指向的对象。在您的本地变量 P 不指向任何东西(= NULL <$ C C $>)。因为你在抽奖使用全局变量 P ,并设置为null。

At your MyLocation class within onLocationChanged method you should write instead of GeoPoint p = new GeoPoint(lat, lon); try p = new GeoPoint(lat, lon);. In your code you created new variable called p (same as global variable) and your local variable point to object. Where your local variable p doesn't point at anything (= null). Because you use your global variable p in draw and is set to null.

这篇关于如何把标记在地图上的位置时,在Android中得到改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆