温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

从json获取坐标,生成地图覆盖物

发布时间:2020-05-12 13:31:48 来源:网络 阅读:1574 作者:愚人cc 栏目:移动开发

从json获取坐标,生成地图覆盖物


package com.chencheng.meituan.view;


import java.util.ArrayList;


import com.baidu.location.BDLocation;

import com.baidu.location.BDLocationListener;

import com.baidu.location.LocationClient;

import com.baidu.location.LocationClientOption;

import com.baidu.mapapi.map.BaiduMap;

import com.baidu.mapapi.map.BitmapDescriptor;

import com.baidu.mapapi.map.BitmapDescriptorFactory;

import com.baidu.mapapi.map.MapStatusUpdate;

import com.baidu.mapapi.map.MapStatusUpdateFactory;

import com.baidu.mapapi.map.MapView;

import com.baidu.mapapi.map.MarkerOptions;

import com.baidu.mapapi.map.MyLocationData;

import com.baidu.mapapi.map.OverlayOptions;

import com.baidu.mapapi.model.LatLng;

import com.chencheng.model.Goodlist;

import com.yuchen.meituan.R;

import com.yuchen.meituan.R.id;

import com.yuchen.meituan.R.layout;

import com.yuchen.meituan.R.menu;


import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.TextView;


public class MapActivity extends Activity implements OnClickListener {


public MyLocationListenner myListener = new MyLocationListenner();

private MapView mMapView;

private BaiduMap mBaiduMap;

private LocationClient mLocClient;

boolean isFirstLoc = true;// 是否首次定位

private Goodlist mGoodlist;

BitmapDescriptor bd = BitmapDescriptorFactory

.fromResource(R.drawable.icon_gcoding);

private ArrayList<BitmapDescriptor> mOverLayList = new ArrayList<BitmapDescriptor>();

private double mLng;

private double mLat;

private float scale = 15.0f;

private BDLocation mLocation;

private TextView mTvProduct;

private TextView mTvTitle;


/**

* 定位SDK监听函数

*/

public class MyLocationListenner implements BDLocationListener {


@Override

public void onReceiveLocation(BDLocation location) {

mLocation = location;

// map view 销毁后不在处理新接收的位置

if (location == null || mMapView == null)

return;

MyLocationData locData = new MyLocationData.Builder()

.accuracy(location.getRadius())

// 此处设置开发者获取到的方向信息,顺时针0-360

.direction(100).latitude(location.getLatitude())

.longitude(location.getLongitude()).build();

mBaiduMap.setMyLocationData(locData);

if (isFirstLoc) {

//设置定位的坐标

isFirstLoc = false;

LatLng ll = new LatLng(mLat,mLng);

MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);

//设置动画

mBaiduMap.animateMapStatus(u);

}

}

public void onReceivePoi(BDLocation poiLocation) {

}

}


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_map);

//获取店铺坐标

mGoodlist = (Goodlist) getIntent().getSerializableExtra("goodsList");

mLng = mGoodlist.getLng();

mLat = mGoodlist.getLat();

initUI();

initMap();

setData();

}


private void setData() {

String product = mGoodlist.getProduct();

String title = mGoodlist.getTitle();

mTvProduct.setText(product);

mTvTitle.setText(title);

}


private void initUI() {

findViewById(R.id.rl_bottom).setOnClickListener(this);

findViewById(R.id.img_add).setOnClickListener(this);

findViewById(R.id.img_reduce).setOnClickListener(this);

findViewById(R.id.img_local).setOnClickListener(this);

findViewById(R.id.rl_back_btn).setOnClickListener(this);

mTvProduct = (TextView) findViewById(R.id.tv_product);

mTvTitle = (TextView) findViewById(R.id.tv_title);

}


@Override

public void onClick(View v) {

MapStatusUpdate msu = null;

switch (v.getId()) {

case R.id.img_add:

//放大

if(scale < 10.0f){

scale = 10.0f;

}

if(scale >= 10.0f && scale <= 20.0f){

scale++;

}

msu = MapStatusUpdateFactory.zoomTo(scale);

mBaiduMap.setMapStatus(msu);

break;

case R.id.img_reduce:

//缩小

if(scale > 20.0f){

scale = 20.0f;

}

if(scale >= 10.0f && scale <= 20.0f){

scale--;

}

msu = MapStatusUpdateFactory.zoomTo(scale);

mBaiduMap.setMapStatus(msu);

break;

case R.id.img_local:

//定位到当前位置

LatLng ll = new LatLng(mLocation.getLatitude(),mLocation.getLongitude());

MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);

//设置动画

mBaiduMap.animateMapStatus(u);

break;

case R.id.rl_back_btn:

finish();

break;

case R.id.rl_bottom:

//拦截监听

break;

default:

break;

}

}

private void initMap() {

// 地图初始化

mMapView = (MapView) findViewById(R.id.bmapView);

mBaiduMap = mMapView.getMap();

// 设置比例尺,15.0f(500米),14.0f(1公里),10.0f(20公里)

MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(scale);

mBaiduMap.setMapStatus(msu);

//添加店铺坐标

addOverlay(mLat, mLng, R.drawable.shop_icon);

// 开启定位图层

mBaiduMap.setMyLocationEnabled(true);

// 定位初始化

mLocClient = new LocationClient(this);

mLocClient.registerLocationListener(myListener);

LocationClientOption option = new LocationClientOption();

option.setOpenGps(true);// 打开gps

option.setCoorType("bd09ll"); // 设置坐标类型

option.setScanSpan(1000);

mLocClient.setLocOption(option);

mLocClient.start();

}


private void addOverlay(double lat, double lng, int drawableRes) {

LatLng llA = new LatLng(lat, lng);

//设置悬浮的图案

BitmapDescriptor bdA = BitmapDescriptorFactory

.fromResource(drawableRes);

mOverLayList.add(bdA);

OverlayOptions ooA = new MarkerOptions().position(llA).icon(bdA)

.zIndex(9).draggable(true);

//在地图中添加悬浮图案

mBaiduMap.addOverlay(ooA);

//设置悬浮物点击监听

// mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

// public boolean onMarkerClick(final Marker marker) {

// Button button = new Button(getApplicationContext());

// button.setBackgroundResource(R.drawable.popup);

// button.setText("更改图标");

// button.setOnClickListener(new OnClickListener() {

// public void onClick(View v) {

// marker.setIcon(bd);

// mBaiduMap.hideInfoWindow();

// }

// });

// LatLng ll = marker.getPosition();

// InfoWindow mInfoWindow = new InfoWindow(button, ll, -47);

// mBaiduMap.showInfoWindow(mInfoWindow);

// return true;

// }

// });

}


@Override

protected void onPause() {

mMapView.onPause();

super.onPause();

}


@Override

protected void onResume() {

mMapView.onResume();

super.onResume();

}


@Override

protected void onDestroy() {

mBaiduMap.setMyLocationEnabled(false);

mMapView.onDestroy();

mMapView = null;

super.onDestroy();

for (int i = 0; i < mOverLayList.size(); i++) {

mOverLayList.get(i).recycle();

}

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return false;

}

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI