在Google Map上添加小图片(转)

首先:这是一个添加图标的工具类。

public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}

@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();
}
}

其次:在另一个继承MapActivity类的类中申明其变量

package com.macrosoft.gobacktoit;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;
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.Overlay;
import com.google.android.maps.OverlayItem;

public class Getlocation extends MapActivity {
private LocationManager lm;
private Location l;
private String strLocationProvider;
private ArrayList<Double> latlon = new ArrayList<Double>();
private double geoLatitude = 0.0;
private double geoLongitude = 0.0;
private static final SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy.MM.dd");
private String date = sdf.format(new Date());
private TextView addr;
private ArrayList<String> numl = new ArrayList<String>();
private ArrayList<String> numf = new ArrayList<String>();
private MapController mMapController01;
private MapView mMapView01;
private int intZoomLevel = 18;
private GeoPoint fromGeoPoint;
private String address = "";
private DBAdapter db;
private float f = 1.0f;
private long lo = 3000L;
public TimeHandler thandler;
private int flag=0;
ZoomControls mZoom;
LinearLayout linearLayout;
List<Overlay> mapOverlays;
Drawable drawable;
HelloItemizedOverlay itemizedoverlay;
@Override
protected boolean isRouteDisplayed() {
return false;
}

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.getlocation);

mMapView01 = (MapView) findViewById(R.id.myMapView1);
addr = (TextView) findViewById(R.id.address);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mZoom = (ZoomControls) mMapView01.getZoomControls();
        linearLayout.addView(mZoom);
        
        mapOverlays = mMapView01.getOverlays();
        
        drawable = this.getResources().getDrawable(R.drawable.hq);
        
        
        itemizedoverlay = new HelloItemizedOverlay(drawable);
        
        
db = new DBAdapter(this);
db.open();

Cursor cn = db.getTitlenums();
cn.moveToFirst();
if (cn.getCount() != 0) {
numf.add(cn.getString(0));
numl.add(cn.getString(1));
lo = Long.parseLong(numl.get(0));
f = Float.parseFloat(numf.get(0));
}
addr.setTextSize(14);
addr.setText("GPS is locating your place ...");

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
l = getLocationProvider(lm);
lm.requestLocationUpdates(strLocationProvider, lo, f,
locationListener);
processLocationUpdated(l);
fromGeoPoint = getGeoByLocation(l);
address = getAddressbyGeoPoint(fromGeoPoint);
refreshMapViewByGeoPoint(fromGeoPoint, mMapView01, 14);

mMapView01.setStreetView(true);
mMapView01.setTraffic(true);
mMapController01 = mMapView01.getController();
mMapController01.setZoom(intZoomLevel);
if(l==null){
Timeout t = new Timeout(this);
thandler = new TimeHandler(this);
t.start();
}
}

public Location getLocationProvider(LocationManager lm) {
Location retLocation = null;
try {
Criteria mCriteria = new Criteria();
mCriteria.setAltitudeRequired(false);
mCriteria.setBearingRequired(false);
mCriteria.setCostAllowed(true);
mCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
mCriteria.setPowerRequirement(Criteria.POWER_LOW);
List<String> l = lm.getAllProviders();
for (int i = 0; i < l.size(); i++) {
if("gps".equals(l.get(i))){
strLocationProvider = l.get(i);
retLocation = lm.getLastKnownLocation("gps");
}
}
if(retLocation==null){
Criteria mCriteria01 = new Criteria();
mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria01.setAltitudeRequired(false);
mCriteria01.setBearingRequired(false);
mCriteria01.setCostAllowed(true);
mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
List<String> l_ = lm.getAllProviders();
for (int i = 0; i < l_.size(); i++) {
if("network".equals(l_.get(i))){
strLocationProvider = l.get(i);
retLocation = lm.getLastKnownLocation("gps");
}
}
retLocation = lm.getLastKnownLocation("network");
}
} catch (Exception e) {
e.printStackTrace();
}
return retLocation;
}

private final LocationListener locationListener = new LocationListener() {

public void onLocationChanged(Location locations) {
processLocationUpdated(locations);
address = getAddressbyGeoPoint(fromGeoPoint);
latlon = getLatandLon(locations);
geoLatitude = latlon.get(0);
geoLongitude = latlon.get(1);
addr.setTextSize(14);
if (address.hashCode() == 964577130) {
addr.setText("Address Not Found!");
} else {
try {
 OverlayItem overlayitem = new OverlayItem(fromGeoPoint, "", "");
 itemizedoverlay.addOverlay(overlayitem);
 mapOverlays.add(itemizedoverlay);
Toast.makeText(Getlocation.this, "The place is marked!",
2).show();
addr.setText(address);
db.updateTitle(1, date, address, latlon.get(1).toString(),
latlon.get(0).toString());
Intent i = new Intent();
i.setClass(Getlocation.this, Goback.class);
startActivity(i);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

public void onStatusChanged(String provider, int status, Bundle extras) {

}
};

private void processLocationUpdated(Location location) {
fromGeoPoint = getGeoByLocation(location);

refreshMapViewByGeoPoint(fromGeoPoint, mMapView01, intZoomLevel);

}

public String getAddressbyGeoPoint(GeoPoint gp) {
String strReturn = "", line1 = null, line2 = null;
try {

if (gp != null) {

Geocoder gc = new Geocoder(Getlocation.this, Locale
.getDefault());

double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;

List<Address> lstAddress = gc.getFromLocation(geoLatitude,
geoLongitude, 1);

if (lstAddress.size() > 0) {
Address adsLocation = lstAddress.get(0);
line1 = adsLocation.getAddressLine(0);
if (line1.length() > 33) {
line1 = line1.substring(0, 28) + " ...";
}

line2 = adsLocation.getAddressLine(1);
if (line2.length() > 33) {
line2 = line2.substring(0, 28) + " ...";
}
}
strReturn = line1 + "/n" + line2;
}
} catch (Exception e) {
e.printStackTrace();
}
return strReturn;
}

public ArrayList<Double> getLatandLon(Location location) {
ArrayList<Double> add = new ArrayList<Double>();
try {
/*** 当Location存在 */
if (location != null) {
geoLatitude = location.getLatitude() * 1E6;
geoLongitude = location.getLongitude() * 1E6;
add.add(geoLatitude);
add.add(geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return add;
}

public GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
try {
/*** 当Location存在 */
if (location != null) {
geoLatitude = location.getLatitude() * 1E6;
geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}

public void refreshMapViewByGeoPoint(GeoPoint gp, MapView mapview,
int zoomLevel) {
try {
mapview.displayZoomControls(true);
MapController myMC = mapview.getController();
myMC.animateTo(gp);
myMC.setZoom(zoomLevel);
mapview.setSatellite(false);
} catch (Exception e) {
e.printStackTrace();
}
}

public void openOptionDialog1() {
new AlertDialog.Builder(this)
.setTitle("Address Not Found")
.setMessage(
"We did not mark it yet, do you want to cancel?")
.setNegativeButton("No",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
//an back jian hou xuanze no
flag=0;
}
}).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
Intent i = new Intent();
i.setClass(Getlocation.this, Goback.class);
startActivity(i);
finish();
}
}).show();
}
public void openOptionDialog() {
new AlertDialog.Builder(this)
.setTitle("Address Not Found")
.setMessage(
"GPS signal is low, continue?")
.setNegativeButton("No",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
Intent i = new Intent();
i.setClass(Getlocation.this, Goback.class);
startActivity(i);
finish();
}
}).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
}
}).show();
}

@Override
protected void onDestroy() {
lm.removeUpdates(locationListener);
db.close();
super.onDestroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (KeyEvent.KEYCODE_BACK == keyCode) {
flag=1;
if(address.hashCode() == 964577130 || "".equals(address)){
openOptionDialog1();
return true;
}
else{
try {
Thread.sleep(2000);
Intent i = new Intent();
i.setClass(Getlocation.this, Goback.class);
startActivity(i);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.onKeyDown(keyCode, event);
}
}else{
return false;
}
}
class Timeout extends Thread{
Getlocation me;
public Timeout(Getlocation g) {
me = g;
}

@Override
public void run() {
while(true){
try {
sleep(lo+5000);
//mei you an back jian
if(flag==0){
me.thandler.sendEmptyMessage(0);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class TimeHandler extends Handler{
public TimeHandler(Getlocation g) {
super();
}

@Override
public void handleMessage(Message msg) {
try {
openOptionDialog();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值