准备工作就是下载谷歌提供的架包,下载地址是:http://code.google.com/p/ksoap2-android/downloads/detail?name=ksoap2-android-assembly-2.4-jar-with-dependencies.jar&can=2&q=大家自己下载,然后导入到工程里面,对了,建立工程时候,版本要
x.x google api的。
第一步,现在写layout目录下一个main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0aVeWdF7g8mo7Q-S3rDHcpmANgEF9752OHJlh0g"
android:clickable="true"
android:state_enabled="true" />
</LinearLayout>
第二步,用来创建一个显示气泡视图的view.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pop" >
<TextView
android:id="@+id/map_bubbleTitle"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center_horizontal"
android:singleLine="true" />
<ImageView
android:id="@+id/map_bubbleImage"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/map_bubbleTitle" />
<TextView
android:id="@+id/map_bubbleText"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/map_bubbleTitle"
android:singleLine="false" />
</RelativeLayout>
第三步,自定义一个ItemizedOverlay用来表示景点的图片和文字的图层,主要针对该图层进行重新绘制,达到想要的效果
package mars.com;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
private Context context;
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
this.context = context;
}
public void addOverlay(OverlayItem overlay) {
overlayItemList.add(overlay);
populate();// 增加新的的overlay一定要使用这个方法,它会读出每一个overlay并准备它被贴上
}
@Override
protected OverlayItem createItem(int i) {
return overlayItemList.get(i);
}
@Override
public int size() {
return overlayItemList.size();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
// projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标之间的转换
Projection projection = mapView.getProjection();
// 遍历所有的OverlayItem
for (int index = this.size() - 1; index > 0; index--) {
// 得到给定索引的item
OverlayItem overlayItem = getItem(index);
// 把经纬度变换到相对于mapview左上角的车屏幕像素坐标
Point point = projection.toPixels(overlayItem.getPoint(), null);
Paint paintText = new Paint();
paintText.setColor(Color.RED);
paintText.setTextSize(13);
// 绘制文本
canvas.drawText(overlayItem.getTitle(), point.x + 10, point.y - 15,
paintText);
}
}
@Override
protected boolean onTap(int index) {
setFocus(overlayItemList.get(index));
return super.onTap(index);
}
}
第四步,实现自己的activity先在地图上根据经纬度创建位置,并且自定义图片和文字进行表示,给自定义的iitemizedoverlay添加焦点改变事件监听,并完成气泡view的显示效果。
package mars.com;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class GoogleMapDemoActivity extends MapActivity {
private MapView mapview;
private View popView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapview = (MapView) findViewById(R.id.mapview);
mapview.setBuiltInZoomControls(true);
// 贴上标记
popView = View.inflate(this, R.layout.view, null);
mapview.addView(popView, new MapView.LayoutParams(
MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT, null,
MapView.LayoutParams.BOTTOM_CENTER));// 由于气泡下边居中,所以设置BOTTOM_CENTER
// 这里没有给GeoPoint在onfocusChangeListener中设置
popView.setVisibility(View.GONE);
// 创建图标资源
Drawable drawable = this.getResources().getDrawable(
R.drawable.ic_launcher);
// 为标记定义边界
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
MyItemizedOverlay overlay = new MyItemizedOverlay(drawable, this);
overlay.setOnFocusChangeListener(onFocusChangeListener);
// 构造一个经纬点,深圳世界之窗
GeoPoint point = new GeoPoint((int) (22.5348 * 1E6),
(int) (113.97246 * 1E6));
OverlayItem overlayItem = new OverlayItem(point, "世界之窗",
"大型文化旅游景区,是深圳著名的旅游景点");
overlay.addOverlay(overlayItem);
// 第二个标记,锦绣中华
point = new GeoPoint((int) (22.5308 * 1E6), (int) (113.99151 * 1E6));
overlayItem = new OverlayItem(point, "锦绣中华",
"中国旅游胜地四十佳之一,是目前世界上最大的实景微缩景区,已经入选中国世界记录协会的候选世界记录");
overlay.addOverlay(overlayItem);
// 添加自定义的ItemizedOvewrlay
List<Overlay> mapOverlays = mapview.getOverlays();
mapOverlays.add(overlay);
// 设置地图模式为交通模式
mapview.setStreetView(true);
// 取得地图控制器对象,用于控制mapView
mapview.getController().setCenter(point);
// 设置默认缩放级别
mapview.getController().setZoom(15);
}
// 当一个overlay焦点发生改变的时候触发
private final ItemizedOverlay.OnFocusChangeListener onFocusChangeListener = new ItemizedOverlay.OnFocusChangeListener() {
public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
if (popView != null) {
popView.setVisibility(View.GONE);
}
if (newFocus != null) {
MapView.LayoutParams geoLP = (LayoutParams) popView
.getLayoutParams();
geoLP.point = newFocus.getPoint();// 用于popView的定位
TextView title = (TextView) popView
.findViewById(R.id.map_bubbleTitle);
title.setText(newFocus.getTitle());
TextView desc = (TextView) popView
.findViewById(R.id.map_bubbleText);
if (newFocus.getSnippet() == null) {
desc.setVisibility(View.GONE);
} else {
desc.setVisibility(View.VISIBLE);
desc.setText(newFocus.getSnippet());
}
mapview.updateViewLayout(popView, geoLP);
popView.setVisibility(View.VISIBLE);
}
}
};
@Override
protected boolean isRouteDisplayed() {// 是否导航
return false;// 不导航
}
}
第五步,别忘了加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".GoogleMapDemoActivity"
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>
</manifest>
后记,我用真机上测试过了,可以使用,但是有一点我不太明白,为什么速度有点慢呢,我可是用的无线网呀(上网页超级快的),缩放的时候,总是等几秒,它的页面才会打开,让人很郁闷,不知道什么情况,不如我的上一个小例子,速度来的快。其他地方倒是没有任何问题。大家作为参考吧。
分享到:
相关推荐
Xilinx Vitis运行报错 2022.2 Vitis: ERROR : Can't read "map": no such variable when trying to launch application on my target
开发百度地图、标注、打开或关闭信息窗口、缩放、单击缩放、双击缩放、绘制地图
googleMap__谷歌___地图_.ppt
google map 的应用实例,可以使用离线地图,很实用
很好的谷歌地图(Google Maps、Google地图)开发例子
谷歌地图Google Map API中文开发文档 V3
Google Map API 接口调用。
googlemap谷歌地图gps纠偏数据库免费下载 纠偏数据库(google地图) 精度为0.01:经纬度每变化0.01,取一次偏移值,共980万条,access文件约600mb,误差5~10米。 精度为0.001:经纬度每变化0.001,取一次偏移值,共9...
vue2-googlemap:基于 Vue 2.x 和google地图组件
GoogleMap谷歌地图demo,调用GoogleMap api 实现的功能全面的地图工具
googlemap 添加标记 并有气泡效果 可添加多个不同的标记 解压后2个文件 android-mapviewballoons 是第三方类库 MallFinderP2 是使用例子
editorjs-googlemap Editor.js 谷歌地图内嵌工具特征允许在您的文章中添加谷歌地图预览。安装通过 NPM 安装获取包裹npm i --save-dev editorjs-googlemap 在您的应用程序中包含模块 const GoogleMap = require ( '...
安卓 android google map 谷歌地图
谷歌地图(googlemap)定位,根据附近wifi热点定位,根据经纬度,解析当前详细地址信息
S40通用的google map/中国谷歌卫星地图
该实例实现google map和ExtJs结合开发,实现图层管理、图层可见性控制、图层要素详细信息查看等功能。
Laravel 地图服务器包安装添加到 composer.json require : {"taviroquai/mapserver" : "~2.0"} 在 app/config/app.php 上添加服务提供者'providers' => array (. . .'Taviroquai\Mapserver\MapserverServiceProvider...
下载googlemap,主要用于方便各位下载google地图
google map v2版本下的地图定位,可是有问题,在有的手机上面可以显示地图,有的手机上面就不能显示地图
然后,特征F用于预测概率图(probability map P)和阈值图(threshold map T) 最后,通过P和F计算近似二值图(approximate binary map B) 在训练期间对P,T,B进行监督训练,P和B是用的相同的监督信号(label)。在...