GoogleMap(通过输入地址,查询显示在google 地图上)

主Activity:

package net.blogjava.mobile.map;

import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
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;

public class Main extends MapActivity {
private MapController mapController;
private GeoPoint geoPoint;
List<Address> addresses;

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

// 用于显示地图的一个ViewGroup
MapView mapView = (MapView) findViewById(R.id.mapview);

// 使得这个View可以获得点击事件
mapView.setClickable(true);

// 设置是否可以进行自动缩放控制
mapView.setBuiltInZoomControls(true);

// 获取控制缩放的操作对象
mapController = mapView.getController();

// 通过系统默认区域设置进行地图定位
Geocoder gc = new Geocoder(this);
// mapView.setTraffic(true);
// mapView.setSatellite(true);
mapView.setStreetView(true);

try {
// 通过地址名称描述返回一个查询结果的数组(后面参数为返回最大结果数)
addresses = gc.getFromLocationName("荆州市江陵中学", 5);

// 如果未查询到任何结果
if (addresses != null) {
geoPoint = new GeoPoint(
// 返回纬度,经度
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));

setTitle(addresses.get(0).getFeatureName());
MyOverlay myOverlay = new MyOverlay();
mapView.getOverlays().add(myOverlay);
mapController.setZoom(20);
mapController.animateTo(geoPoint);
}
} catch (Exception e) {
e.printStackTrace();
}
}


@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

class MyOverlay extends Overlay {

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Paint paint = new Paint();
paint.setColor(Color.RED);
Point screenPoint = new Point();
mapView.getProjection().toPixels(geoPoint, screenPoint);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.flag);
canvas.drawBitmap(bmp, screenPoint.x, screenPoint.y, paint);
canvas.drawText(addresses.get(0).getFeatureName().toString(),
screenPoint.x, screenPoint.y, paint);
return super.draw(canvas, mapView, shadow, when);
}

}

}

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.blogjava.mobile.map" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0y_0zdWEE7Cd9rf2h8PcSNl2itJSrEaOMLSIJ1g" />
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值