一个根据GPS信息在地图上的定位的小demo
布局文件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">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView android:text="经度" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/lng" android:layout_width="85px"
android:layout_height="wrap_content" />
<TextView android:text="纬度" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/lat" android:layout_width="85px"
android:layout_height="wrap_content" />
<Button android:id="@+id/loc" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="定位"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_horizontal">
<RadioGroup android:id="@+id/rg" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1">
<RadioButton android:text="普通" android:id="@+id/normal"
android:checked="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton android:text="卫星" android:id="@+id/satellite"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mv" android:clickable="true" android:enabled="true"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:apiKey="0kcrUw1E0GWVCoo6chWBqqxAxiLQGpqgcTII6HQ" />
</LinearLayout>
主界面文件
package com.hc;
import java.util.List;
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 android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class GooglemapActivity extends MapActivity {
/** Called when the activity is first created. */
Button locBn;
RadioGroup mapType;
MapView mv;
EditText etLng, etLat;
MapController controller;
Bitmap posBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
posBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
mv = (MapView) findViewById(R.id.mv);
etLng = (EditText) findViewById(R.id.lng);
etLat = (EditText) findViewById(R.id.lat);
// 显示放大缩小控制按钮
mv.setBuiltInZoomControls(true);
controller = mv.getController();
locBn = (Button) findViewById(R.id.loc);
locBn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String lng = etLng.getEditableText().toString().trim();
String lat = etLat.getEditableText().toString().trim();
if (lng.equals("") || lat.equals("")) {
Toast.makeText(GooglemapActivity.this, "输入有效经度维度",
Toast.LENGTH_LONG).show();
} else {
double dlong = Double.parseDouble(lng);
double dLat = Double.parseDouble(lat);
UpdateMapView(dlong, dLat);
}
}
});
locBn.performClick();
mapType = (RadioGroup) findViewById(R.id.rg);
mapType.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.normal:
mv.setSatellite(false);
break;
case R.id.satellite:
mv.setSatellite(true);
break;
}
}
});
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return true;
}
private void UpdateMapView(double dlong, double dLat) {
GeoPoint gp = new GeoPoint((int)(dLat*1E6), (int)(dlong*1E6));
mv.displayZoomControls(true);
controller.animateTo(gp);
List<Overlay> ol = mv.getOverlays();
ol.clear();
ol.add(new PosOverLay(gp, posBitmap));
}
}
地图上显示的自定义定位图片
package com.hc;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class PosOverLay extends Overlay {
Bitmap posBitmap;
GeoPoint gp;
public PosOverLay(GeoPoint gp,Bitmap posBitmap){
super();
this.gp=gp;
this.posBitmap=posBitmap;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if(!shadow){
Projection proj = mapView.getProjection();
Point p= new Point();
proj.toPixels(gp, p);
canvas.drawBitmap(posBitmap, p.x-posBitmap.getWidth()/2,p.y-posBitmap.getHeight(), null);
}
}
}
manifest文件中加入两条
<uses-permission android:name="android.permission.INTERNET"/> 网络权限
<uses-library android:name="com.google.android.maps"/>
效果如下
- 大小: 52.3 KB
分享到:
相关推荐
NULL 博文链接:https://kurting615.iteye.com/blog/1150574
android google map 应用。可以显示地图,提供搜索功能,解析地址并定位。
在android模拟器上可以使用google map,定位自己的位置,也可以定位任何给定的经度和纬度的精确位置,还可以获得各种语言的地理位置信息。
google map v2版本下的地图定位,可是有问题,在有的手机上面可以显示地图,有的手机上面就不能显示地图
Android Google Map v2,继承FragmentActivity 使用MapFragment实现定位 源代码!源工程!
android google map实例,提供搜索资源定位,选择城市定位,地图模式进行选择,需要自行更新googleMapAPI
精心收集的android googlemap开发实例与教程,调用gps定位,以及给定经纬度显示地图信息等示例代码
此资料包含 Android学习笔记 - 地图.doc 通过地名获得经纬度并标识在地图上.doc 点中overlay弹出带尾巴的气泡的实现.doc ...android map.doc Android地图和定位学习总结 .doc 希望对大家有帮助。。。。
基于Google Map 开发android手机地图,通过GPS定位
Android使用Google Map API创建的一个根据经纬度定位的程序. 交流QQ群:1279871
Google_Map_纠偏说明文档.xls
个人写的一个基于googlemap v3web版android地图,实现了定位显示,轨迹播放等功能
可以实现google map 以及定位功能,代码已经调试过了 ,完全可以运行
使用物理定位以及GPS自动定位当前位置,在googleMap中显示
GoogleMap和location,android google地图展现与定位
GPS定位,手机端及电脑端构建tcp/ip连接。 Android 手机端获取实时定位信息,发送到C# 电脑端服务器,并在Google地图上打点显示路径。
本人开发时做的一部分,使用Googlemap定位可用。
关于MAP的开发学习文档。 位置服务(Location-Based Services,LBS),又称定位服务或基于位置的服务,融合了GPS定位、移动通信、导航等多种技术,提供了与空间位置相关的综合应用服务 位置服务首先在日本得到商业...
通过google地图后续开发公交路线.时时定位等。