首页 > 其他 > 详细

AmapDemo仿照一号专车的地图界面

时间:2015-09-17 02:14:41      阅读:430      评论:0      收藏:0      [点我收藏+]

AmapDemo仿照一号专车的地图界面

AmapDemo仿照一号专车的地图界面,地图加载定位进入后的放大效果和地图移动效果非常棒,
本项目指导我们如何使用地图功能,如何自定义系统定位蓝点,本项目主要引导我们如何嵌入
第三方sdk实现地图定位效果。

运行效果:

  • bubuko.com,布布扣

相关代码

?

package cn.xm.weidongjian.amapdemo;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
?* <a href="http://www.itlanbao.com/code/20150915/10000/100530.html Fundamentals</a>
?*/
public class ApplicationTest extends ApplicationTestCase<Application> {
??? public ApplicationTest() {
??????? super(Application.class);
??? }
}

package cn.xm.weidongjian.amapdemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import cn.xm.weidongjian.amapdemo.utils.ToastUtil;


public class AmapActivity extends AppCompatActivity implements AMapLocationListener, AMap.OnCameraChangeListener, View.OnClickListener, LocationSource, OnGeocodeSearchListener {
??? private MapView mapView;
??? private AMap aMap;
??? private LocationManagerProxy mLocationManagerProxy;
??? private Handler handler = new Handler();
??? private OnLocationChangedListener listener;
??? private LatLng myLocation = null;
??? private Marker centerMarker;
??? private boolean isMovingMarker = false;
??? private BitmapDescriptor movingDescriptor, chooseDescripter, successDescripter;
??? private ValueAnimator animator = null;
??? private GeocodeSearch geocodeSearch;
??? private Toolbar toolbar;
??? private FrameLayout containerLayout;
??? private ImageView ivCircle;
??? private TextView tvCurLocation, tvDestination;

??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_amap);
??????? mapView = (MapView) findViewById(R.id.map);
??????? mapView.onCreate(savedInstanceState);

??????? initUI();
??????? initAmap();
??????? setUpLocationStyle();
??? }

??? private void initUI() {
??????? findViewById(R.id.myLocation).setOnClickListener(this);
??????? toolbar = (Toolbar) findViewById(R.id.toolbar);
??????? toolbar.setTitle("现在用车");
??????? toolbar.setTitleTextColor(Color.WHITE);
??????? setSupportActionBar(toolbar);
??????? containerLayout = (FrameLayout) findViewById(R.id.container);
??????? tvCurLocation = (TextView) findViewById(R.id.location);
??????? tvDestination = (TextView) findViewById(R.id.destination);
??????? introAnimPrepare();
??? }

??? private void initAmap() {
??????? if (aMap == null) {
??????????? aMap = mapView.getMap();
??????? }
??????? aMap.setLocationSource(this);// 设置定位监听
??????? aMap.setMyLocationEnabled(true);
??????? aMap.getUiSettings().setZoomControlsEnabled(false);

??????? aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
??????? CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(15);
??????? aMap.moveCamera(cameraUpdate);

??????? movingDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_loaction_choose_moving);
??????? chooseDescripter = BitmapDescriptorFactory.fromResource(R.drawable.icon_loaction_choose);
??????? successDescripter = BitmapDescriptorFactory.fromResource(R.drawable.icon_usecarnow_position_succeed);

??????? geocodeSearch = new GeocodeSearch(this);
??????? geocodeSearch.setOnGeocodeSearchListener(this);
??? }

??? private void setUpLocationStyle() {
??????? // 自定义系统定位蓝点
??????? MyLocationStyle myLocationStyle = new MyLocationStyle();
??????? myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
??????????????? fromResource(R.drawable.img_location_now));
??????? myLocationStyle.strokeWidth(0);
??????? myLocationStyle.radiusFillColor(Color.TRANSPARENT);
??????? aMap.setMyLocationStyle(myLocationStyle);
??? }

??? @Override
??? protected void onResume() {
??????? super.onResume();
??????? mapView.onResume();
??? }

??? @Override
??? protected void onPause() {
??????? super.onPause();
??????? mapView.onPause();
??????? deactivate();
??? }

??? @Override
??? protected void onSaveInstanceState(Bundle outState) {
??????? super.onSaveInstanceState(outState);
??????? mapView.onSaveInstanceState(outState);
??? }

??? @Override
??? protected void onDestroy() {
??????? mapView.onDestroy();
??????? containerLayout.removeView(ivCircle);
??????? super.onDestroy();
??? }

??? @Override
??? public void onLocationChanged(AMapLocation aMapLocation) {
??????? if (aMapLocation != null && aMapLocation.getAMapException().getErrorCode() == 0) {
??????????? if (listener != null) {
??????????????? listener.onLocationChanged(aMapLocation);// 显示系统小蓝点
??????????? }
??????????? myLocation = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
??????????? tvCurLocation.setText(aMapLocation.getRoad() + aMapLocation.getStreet() + aMapLocation.getPoiName());
??????????? addChooseMarker();
??????? }
??? }

??? private void addChooseMarker() {
??????? MarkerOptions centerMarkerOption = new MarkerOptions().position(myLocation).icon(chooseDescripter);
??????? centerMarker = aMap.addMarker(centerMarkerOption);
??????? centerMarker.setPositionByPixels(mapView.getWidth() / 2, mapView.getHeight() / 2);
??????? handler.postDelayed(new Runnable() {
??????????? @Override
??????????? public void run() {
??????????????? CameraUpdate update = CameraUpdateFactory.zoomTo(17f);
??????????????? aMap.animateCamera(update, 1000, new AMap.CancelableCallback() {
??????????????????? @Override
??????????????????? public void onFinish() {
??????????????????????? aMap.setOnCameraChangeListener(AmapActivity.this);
??????????????????? }
??????????????????? @Override
??????????????????? public void onCancel() {
??????????????????? }
??????????????? });
??????????? }
??????? }, 1000);
??? }

??? @Override
??? public void onLocationChanged(Location location) {
??? }

??? @Override
??? public void onStatusChanged(String provider, int status, Bundle extras) {

??? }

??? @Override
??? public void onProviderEnabled(String provider) {

??? }

??? @Override
??? public void onProviderDisabled(String provider) {
??? }

??? @Override
??? public void activate(OnLocationChangedListener onLocationChangedListener) {
??????? listener = onLocationChangedListener;
??????? mLocationManagerProxy = LocationManagerProxy.getInstance(this);
??????? mLocationManagerProxy.requestLocationData(
??????????????? LocationProviderProxy.AMapNetwork, -1, 100, this);
??? }

??? public void deactivate() {
??????? if (mLocationManagerProxy != null) {
??????????? mLocationManagerProxy.removeUpdates(this);
??????????? mLocationManagerProxy.destroy();
??????? }
??????? mLocationManagerProxy = null;
??? }

??? @Override
??? public void onCameraChange(CameraPosition cameraPosition) {
??????? if (centerMarker != null) {
??????????? setMovingMarker();
??????? }
??? }

??? @Override
??? public void onCameraChangeFinish(CameraPosition cameraPosition) {
??????? LatLonPoint point = new LatLonPoint(cameraPosition.target.latitude, cameraPosition.target.longitude);
??????? RegeocodeQuery query = new RegeocodeQuery(point, 50, GeocodeSearch.AMAP);
??????? geocodeSearch.getFromLocationAsyn(query);
??????? if (centerMarker != null) {
??????????? animMarker();
??????? }
??????? showLocationView();
??? }

??? @Override
??? public void onClick(View v) {
??????? switch (v.getId()) {
??????????? case R.id.myLocation:
??????????????? CameraUpdate update = CameraUpdateFactory.changeLatLng(myLocation);
??????????????? aMap.animateCamera(update);
??????????????? break;
??????????? default:
??????????????? break;
??????? }
??? }

??? private void setMovingMarker() {
??????? if (isMovingMarker)
??????????? return;

??????? isMovingMarker = true;
??????? centerMarker.setIcon(movingDescriptor);
??????? hideLocationView();
??? }

??? private void animMarker() {
??????? isMovingMarker = false;
??????? if (animator != null) {
??????????? animator.start();
??????????? return;
??????? }
??????? animator = ValueAnimator.ofFloat(mapView.getHeight()/2, mapView.getHeight()/2 - 30);
??????? animator.setInterpolator(new DecelerateInterpolator());
??????? animator.setDuration(150);
??????? animator.setRepeatCount(1);
??????? animator.setRepeatMode(ValueAnimator.REVERSE);
??????? animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
??????????? @Override
??????????? public void onAnimationUpdate(ValueAnimator animation) {
??????????????? Float value = (Float) animation.getAnimatedValue();
??????????????? centerMarker.setPositionByPixels(mapView.getWidth() / 2, Math.round(value));
??????????? }
??????? });
??????? animator.addListener(new AnimatorListenerAdapter() {
??????????? @Override
??????????? public void onAnimationEnd(Animator animation) {
??????????????? centerMarker.setIcon(chooseDescripter);
??????????? }
??????? });
??????? animator.start();
??? }

??? private void endAnim() {
??????? if (animator != null && animator.isRunning())
??????????? animator.end();
??? }

??? @Override
??? public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
??????? if(i == 0){
??????????? if(regeocodeResult != null&& regeocodeResult.getRegeocodeAddress() != null){
??????????????? endAnim();
??????????????? centerMarker.setIcon(successDescripter);
??????????????? RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
??????????????? String formatAddress = regeocodeResult.getRegeocodeAddress().getFormatAddress();
??????????????? String shortAdd = formatAddress.replace(regeocodeAddress.getProvince(), "").replace(regeocodeAddress.getCity(), "").replace(regeocodeAddress.getDistrict(), "");
??????????????? tvCurLocation.setText(shortAdd);
??????????? }else{
??????????????? ToastUtil.show(AmapActivity.this, R.string.no_result);
??????????? }
??????? }else{
??????????? ToastUtil.show(AmapActivity.this, R.string.error_network);
??????? }
??? }

??? @Override
??? public void onGeocodeSearched(GeocodeResult geocodeResult, int rCode) {
??? }

??? private void introAnimPrepare() {
??????? toolbar.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
??????????? @Override
??????????? public boolean onPreDraw() {
??????????????? toolbar.getViewTreeObserver().removeOnPreDrawListener(this);
??????????????? toolbar.setTranslationY(-toolbar.getHeight());
??????????????? return false;
??????????? }
??????? });
??????? ivCircle = new ImageView(this);
??????? ivCircle.setImageResource(R.drawable.tunahome_imageview_bottom);
??????? FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
??????? params.gravity = Gravity.CENTER;
??????? containerLayout.addView(ivCircle, params);
??????? ivCircle.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
??????????? @Override
??????????? public boolean onPreDraw() {
??????????????? ivCircle.getViewTreeObserver().removeOnPreDrawListener(this);
??????????????? ivCircle.setTranslationY(containerLayout.getHeight() / 2 - ivCircle.getHeight());
??????????????? ivCircle.setScaleX(2f);
??????????????? ivCircle.setScaleY(2f);
??????????????? return false;
??????????? }
??????? });
??????? containerLayout.post(new Runnable() {
??????????? @Override
??????????? public void run() {
??????????????? animIntroduce();
??????????? }
??????? });
??? }

??? private void animIntroduce() {
??????? ObjectAnimator animToolbar = ObjectAnimator.ofFloat(toolbar, "TranslationY", 0f);
??????? animToolbar.setDuration(300);
??????? ObjectAnimator animCircle = ObjectAnimator.ofFloat(ivCircle, "TranslationY", 0);
??????? animCircle.setDuration(400);
??????? ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivCircle, "ScaleX", 1f);
??????? scaleX.setDuration(400);
??????? ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivCircle, "ScaleY", 1f);
??????? scaleY.setDuration(400);
??????? AnimatorSet animatorSet = new AnimatorSet();
??????? animatorSet.playTogether(animToolbar, animCircle, scaleX, scaleY);
??????? animatorSet.addListener(new AnimatorListenerAdapter() {
??????????? @Override
??????????? public void onAnimationEnd(Animator animation) {
??????????????? containerLayout.removeView(ivCircle);
??????????????? mapView.setVisibility(View.VISIBLE);
??????????????? tvCurLocation.setVisibility(View.VISIBLE);
??????????????? tvDestination.setVisibility(View.VISIBLE);
??????????? }
??????? });
??????? animatorSet.start();
??? }

??? private void hideLocationView() {
??????? ObjectAnimator animLocation = ObjectAnimator.ofFloat(tvCurLocation, "TranslationY", -tvCurLocation.getHeight()*2);
??????? ObjectAnimator animDestinatiion = ObjectAnimator.ofFloat(tvDestination, "TranslationY", tvDestination.getHeight()*2);
??????? AnimatorSet set = new AnimatorSet();
??????? set.playTogether(animDestinatiion, animLocation);
??????? set.setDuration(200);
??????? set.start();
??? }

??? private void showLocationView() {
??????? ObjectAnimator animLocation = ObjectAnimator.ofFloat(tvCurLocation, "TranslationY", 0);
??????? ObjectAnimator animDestinatiion = ObjectAnimator.ofFloat(tvDestination, "TranslationY", 0);
??????? AnimatorSet set = new AnimatorSet();
??????? set.playTogether(animDestinatiion, animLocation);
??????? set.setDuration(200);
??????? set.start();
??? }
}

package cn.xm.weidongjian.amapdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
?* Created by Weidongjian on 2015/7/6.
?*/
public class MainActivity extends AppCompatActivity {

??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
??????????? @Override
??????????? public void onClick(View v) {
??????????????? startAmap();
??????????? }
??????? });
??? }

??? private void startAmap() {
??????? Intent intent = new Intent(MainActivity.this, AmapActivity.class);
??????? intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
??????? startActivity(intent);
??????? overridePendingTransition(0, 0);
??? }
}

?

完整的代码下载:http://www.itlanbao.com/code/20150915/10000/100530.html

AmapDemo仿照一号专车的地图界面

原文:http://yichen9867.iteye.com/blog/2243779

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!