战平经度和纬度上的谷歌地图引脚/ MapOverlays从对象类 [英] Draw Pins/MapOverlays on Google Maps with latitude and longitude from an Object Class

查看:124
本文介绍了战平经度和纬度上的谷歌地图引脚/ MapOverlays从对象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有一个展示地图和把用户的当前位置的信号,这个 MapActivity 类。
在另一方面,我有一个对象类VideoLocation其中有哪些是从服务器的JSON文件中提取一些地方的经度和纬度。我也想有位置与地图上绘制针

我怎么能做到这一点?我需要什么,以我的类下面添加?

FYI这是我如何调用该对象的经度和纬度在我的ListActivity:

  VideoLocation vidLocation = videoLocations [位置]双LAT2 = vidLocation.latitude;
双lng2 = vidLocation.longitude;

本类:

 公共类MyMapActivity扩展MapActivity实现OnClickListener {
        公共静态最后的字符串标记=GoogleMapsActivity;
        私人MapView类MapView类;
        私人的LocationManager的LocationManager;
        地理codeR地理codeR;
        地点位置;
        LocationListener的LocationListener的;
        CountDownTimer locationtimer;
        MapController mapController;
        MapOverlay mapOverlay =新MapOverlay();        私人VideoLocation [] videoLocations = NULL;        @覆盖
        保护无效的onCreate(捆绑冰柱){
            super.onCreate(冰柱);
            的setContentView(R.layout.my_map_activity);
            的initComponents();            JsonDB dbhelper =新JsonDB(MyMapActivity.this);
            SQLiteDatabase分贝= dbhelper.getWritableDatabase();            db.beginTransaction();
            videoLocations = dbhelper.getVideoLocations(DB);
            db.setTransactionSuccessful(); //结束交易
            db.endTransaction();
            db.close();            最终的ImageButton刷新=(的ImageButton)findViewById(R.id.btn_nav_im pressum);
            refresh.setOnClickListener(本);            最终的ImageButton搜索=(的ImageButton)findViewById(R.id.btn_nav_locater);
            sea​​rch.setOnClickListener(本);            的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE);
            如果(的LocationManager == NULL){
                Toast.makeText(MyMapActivity.this,
                        位置管理器不可用,Toast.LENGTH_SHORT)
                        。显示();
                返回;
            }
            位置=的LocationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            如果(位置== NULL)
                位置=的LocationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            如果(位置!= NULL){                animateMap(位置);
                清单<&叠加GT; listOfOverlays =调用MapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);
            }
            LocationListener的=新LocationListener的(){
                @覆盖
                公共无效onStatusChanged(字符串为arg0,ARG1 INT,捆绑ARG2){
                }                @覆盖
                公共无效onProviderEnabled(字符串为arg0){
                }                @覆盖
                公共无效onProviderDisabled(字符串为arg0){
                }                @覆盖
                公共无效onLocationChanged(位置L){
                    位置= 1;
                    locationManager.removeUpdates(本);
                    如果(l.getLatitude()== 0 || l.getLongitude()== 0){
                    }其他{
                        双纬度= l.getLatitude();
                        双LNG = l.getLongitude();
                        showAddress(纬度,经度);
                    }
                }
            };
            如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,1000,10F,LocationListener的);
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,1000,10F,LocationListener的);
            locationtimer =新CountDownTimer(30000,5000){
                @覆盖
                公共无效onTick(长millisUntilFinished){
                    如果(位置!= NULL)
                        locationtimer.cancel();
                }                @覆盖
                公共无效onFinish(){
                    如果(位置== NULL){
                    }
                }
            };
            locationtimer.start();
        }        公共无效animateMap(地点){
        双纬度= location.getLatitude();
            双LNG = location.getLongitude();            的GeoPoint点=新的GeoPoint((INT)(LAT * 1E6),(INT)(LNG * 1E6));
            的GeoPoint点2 =新的GeoPoint((INT)(LAT2 * 1E6),(INT)(lng2 * 1E6));
            mapController.animateTo(点,新的Message());
            mapOverlay.setPointToDraw(点);        }
        公共图形页面getMapView(){
            返回this.mapView;
        }        私人无效的initComponents(){
            图形页面=(图形页面)findViewById(R.id.mapView);
            mapView.setBuiltInZoomControls(真);
            mapController = mapView.getController();
            mapController.setZoom(16);
        }        @覆盖
        保护布尔isRouteDisplayed(){
            返回false;
        }        类MapOverlay扩展覆盖{
            私人的GeoPoint pointToDraw;            公共无效setPointToDraw(GeoPoint的点){
                pointToDraw =点;
            }            公众的GeoPoint getPointToDraw(){
                返回pointToDraw;
            }            @覆盖
            公共布尔平局(帆布帆布,MapView类MapView类,布尔阴影,
                    时长){
                super.draw(画布,MapView类,阴影);                点screenPts =新点();
                。MapView.getProjection()在toPixels(pointToDraw,screenPts);                BMP位图= BitmapFactory.de codeResource(getResources()
                        R.drawable.pin_normal);
                canvas.drawBitmap(BMP,screenPts.x,screenPts.y - 24,NULL);
                返回true;
            }


解决方案

如果你想在你的地图上添加针(或图片)将ItemizedOverlay类会更好。结果
包com.android.testandroidmap;

 进口的java.util.ArrayList;进口android.app.AlertDialog;
进口android.content.Context;
进口android.graphics.drawable.Drawable;进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.OverlayItem;公共类HelloItemizedOverlay扩展ItemizedOverlay {私人的ArrayList< OverlayItem> mOverlays =新的ArrayList< OverlayItem>();
私人语境mContext;公共HelloItemizedOverlay(可绘制defaultMarker,上下文的背景下){
    超(boundCenterBottom(defaultMarker));
    // TODO自动生成构造函数存根
    mContext =背景;
}@覆盖
保护OverlayItem createItem中(int i)以{
    // TODO自动生成方法存根
    返回mOverlays.get(ⅰ);
}@覆盖
公众诠释大小(){
    // TODO自动生成方法存根
    返回mOverlays.size();
}公共无效addOverlay(OverlayItem叠加){
    mOverlays.add(覆盖);
    填充();
}@覆盖
保护布尔中的onTap(INT指数){
  OverlayItem项目= mOverlays.get(指数);
  AlertDialog.Builder对话框=新AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  返回true;
}
}

我从 Android开发者官方指南,并用这个类在你的code,可以增加覆盖。

添加所有你需要的东西(容器中,销形象,销+的坐标叠加)

 列表<&叠加GT; mapOverlays =调用MapView.getOverlays();
可绘制可绘制= this.getResources()getDrawable(R.drawable.androidmarker)。
HelloItemizedOverlay itemizedoverlay =新HelloItemizedOverlay(绘制,这一点);

为你的图标由前级识别

,也实例化的坐标,添加坐标的overlayItem +注册与HelloItemizedOverlay之前

提出的overlayItem

 的GeoPoint点=新的GeoPoint(19240000,-99120000);
OverlayItem overlayitem =新OverlayItem(点,霍拉,世界报!,我在墨西哥城!);
itemizedoverlay.addOverlay(overlayitem);

光洁度与添加叠加到mapOverlay,这是重叠的集合。

  mapOverlays.add(itemizedoverlay);

这显示在我的地图,可以通过任何你想要的形象来定制上方的脚。如果你想同时使用不同的图像的每个位置,只需实例化图像的另一个可绘制并注册可绘制到其他HelloItemizedOverlay。

I have this MapActivity class below showing a map and putting a pin of the user's current location. In the other hand I have an Object class VideoLocation which has latitude and longitude of some places which are extracted from a JSON file from the server. I also wanna have the locations are drawn with pins on the map.

How am I able to do that? What do I need to add on my Class below?

FYI this is how I call the object's latitude and longitude on my ListActivity:

VideoLocation vidLocation = videoLocations[position];

double lat2 = vidLocation.latitude;
double lng2 = vidLocation.longitude; 

This the class:

public class MyMapActivity extends MapActivity implements OnClickListener {
        public static final String TAG = "GoogleMapsActivity";
        private MapView mapView;
        private LocationManager locationManager;
        Geocoder geocoder;
        Location location;
        LocationListener locationListener;
        CountDownTimer locationtimer;
        MapController mapController;
        MapOverlay mapOverlay = new MapOverlay();

        private VideoLocation[] videoLocations = null;

        @Override
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.my_map_activity);
            initComponents();

            JsonDB dbhelper = new JsonDB(MyMapActivity.this);
            SQLiteDatabase db = dbhelper.getWritableDatabase();

            db.beginTransaction();
            videoLocations = dbhelper.getVideoLocations(db);
            db.setTransactionSuccessful();//end transaction
            db.endTransaction();
            db.close();

            final ImageButton refresh = (ImageButton) findViewById(R.id.btn_nav_impressum);
            refresh.setOnClickListener(this);

            final ImageButton search =(ImageButton) findViewById(R.id.btn_nav_locater);
            search.setOnClickListener(this);



            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (locationManager == null) {
                Toast.makeText(MyMapActivity.this,
                        "Location Manager Not Available", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
                location = locationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {

                animateMap(location);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);
            }
            locationListener = new LocationListener() {
                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                }

                @Override
                public void onProviderEnabled(String arg0) {
                }

                @Override
                public void onProviderDisabled(String arg0) {
                }

                @Override
                public void onLocationChanged(Location l) {
                    location = l;
                    locationManager.removeUpdates(this);
                    if (l.getLatitude() == 0 || l.getLongitude() == 0) {
                    } else {
                        double lat = l.getLatitude();
                        double lng = l.getLongitude();
                        showAddress(lat,lng);
                    }
                }
            };
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
            locationtimer = new CountDownTimer(30000, 5000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (location != null)
                        locationtimer.cancel();
                }

                @Override
                public void onFinish() {
                    if (location == null) {
                    }
                }
            };
            locationtimer.start();
        }

        public void animateMap(Location location){
        double lat = location.getLatitude();
            double lng = location.getLongitude();

            GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            GeoPoint point2 = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));
            mapController.animateTo(point, new Message());
            mapOverlay.setPointToDraw(point);

        }


        public MapView getMapView() {
            return this.mapView;
        }

        private void initComponents() {
            mapView = (MapView) findViewById(R.id.mapView);
            mapView.setBuiltInZoomControls(true);
            mapController = mapView.getController();
            mapController.setZoom(16);
        }

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

        class MapOverlay extends Overlay {
            private GeoPoint pointToDraw;

            public void setPointToDraw(GeoPoint point) {
                pointToDraw = point;
            }

            public GeoPoint getPointToDraw() {
                return pointToDraw;
            }

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

                Point screenPts = new Point();
                mapView.getProjection().toPixels(pointToDraw, screenPts);

                Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_normal);
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
                return true;
            }

解决方案

adding ItemizedOverlay class would be better if you want to add pin (or images) on top of your map.
package com.android.testandroidmap;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class HelloItemizedOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
    mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlays.size();
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}
}

I get this code from Android Developer Official Guide, and with this class on your code, you can add the overlays.

Add all the things you need (container, image for pins, and the pin+coordinate overlay)

List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

for your icons to be recognized by the class before, and also instantiate the coordinate, add the coordinate to an overlayItem + register the overlayItem with the HelloItemizedOverlay you made before

GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);

finish with adding the overlay to the mapOverlay, which is a collection of overlays.

mapOverlays.add(itemizedoverlay);

This displays a pin on top of my map, which can be customized by any image you want. If you want to also use different images for each location, just instantiate another Drawable with your image and register that Drawable to other HelloItemizedOverlay.

这篇关于战平经度和纬度上的谷歌地图引脚/ MapOverlays从对象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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