GoogleMap开发-Android

GoogleMap开发,做个小结,网上很少关于这个的资料,刚学习时,头疼了半天,贴出代码,供大家参考

1、布局文件main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >


 <fragment

        android:id="@+id/map_layout_map"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_margin="0.5dip"

        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>



2、Acivity中

public class MapActivity extends FragmentActivity {

private GoogleMap map;

private SupportMapFragment mapFragment;

 

 

 


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

setContentView(R.layout.main);

initControls();

initMap();

location(application.getLatitude(), application.getLongitude());

displayAllLocation();


}

private void initControls() {

 

FragmentManager manager = getSupportFragmentManager();

mapFragment = (SupportMapFragment) manager

.findFragmentById(R.id.map_layout_map);

map = mapFragment.getMap();

}

/**

* Initialize the map

*/

private void initMap() {

try {

 

map.clear();

map.setMyLocationEnabled(true);

map.setIndoorEnabled(true);

map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

// map.setTrafficEnabled(true);

map.setOnMapLongClickListener(onMapLongClickListener);

// mark视图标记点击事件

map.setOnMarkerClickListener(onMarkerClikcListener);

 

  } catch (Exception e) {

  }

}

/**

  * 定位

  * 

  * 

  */

public void location(double lat, double lng) {

try {

LatLng latLng = new LatLng(lat, lng);

map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

map.moveCamera(CameraUpdateFactory.zoomTo(10));

} catch (Exception e) {

CommonHelper.log(this.getClass().getName(),

"searchLocation:" + e.getMessage());

}

}


 private void displayAllLocation() {

try {

map.clear();

LatLng at = null;



if (entity.getLat() != 0.0 || entity.getLng() != 0.0) {




MarkerOptions options = new MarkerOptions();

BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory

.fromResource(R.drawable.ic_map_loaction);

options.icon(bitmapDescriptor);

LatLng latlng = new LatLng(entity.getLat(), entity.getLng());

options.position(latlng);

options.visible(true);

Marker marker=map.addMarker(options);

at = latlng;

map.moveCamera(CameraUpdateFactory.newLatLng(at));

map.moveCamera(CameraUpdateFactory.zoomTo(10));

 


}



// map.clear();

} catch (Exception e) {

CommonHelper.log(this.getClass().getName(), "displayAllLocation:"

+ e.getMessage());

}

}


 private OnMarkerClickListener onMarkerClikcListener = new OnMarkerClickListener() {


@Override

public boolean onMarkerClick(Marker arg0) {

MapInforWindow info = new MapInforWindow(MapActivity.this);

 

map.setInfoWindowAdapter(info);

 

return false;


}

};

}



3、MapInforWindow

 

public class MapWOInforWindow implements InfoWindowAdapter{
private Context context;

 

public MapWOInforWindow(Context context ) {
this.context = context;
 

}



@Override
public View getInfoContents(Marker arg0) {
return null;
}


@Override
public View getInfoWindow(Marker arg0) {
View view = LayoutInflater.from(context).inflate(
R.layout.map_wo_inforwindow_layout, null);
TextView tvTitle=(TextView)view.findViewById(R.id.map_wo_infowindow_layout_tv_title);
tvTitle.setText("Titlte");
 
         
return view;
}
 
   
}


4、如果希望进地图页面,直接弹出InforWindow,可以这样写

private void displayAllLocation() {

 try {

 map.clear();

 LatLng at = null;



 if (entity.getLat() != 0.0 || entity.getLng() != 0.0) {




 MarkerOptions options = new MarkerOptions();

 BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory

 .fromResource(R.drawable.ic_map_loaction);

 options.icon(bitmapDescriptor);

 LatLng latlng = new LatLng(entity.getLat(), entity.getLng());

 options.position(latlng);

 options.visible(true);

 Marker marker=map.addMarker(options);

 at = latlng;

 map.moveCamera(CameraUpdateFactory.newLatLng(at));

 map.moveCamera(CameraUpdateFactory.zoomTo(10));

  onMarkerClikcListener.onMarkerClick(marker);//加这句

                                 

 


 }



 // map.clear();

 } catch (Exception e) {

 CommonHelper.log(this.getClass().getName(), "displayAllLocation:"

 + e.getMessage());

 }

 }

private OnMarkerClickListener onMarkerClikcListener = new OnMarkerClickListener() {


 @Override

 public boolean onMarkerClick(Marker arg0) {

 MapInforWindow info = new MapInforWindow(MapActivity.this);

 

 map.setInfoWindowAdapter(info);

 

                         arg0.showInfoWindow();//加这句

 return false;


 }

 };






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值