Skip to content

Commit

Permalink
填家选项设置,准备发布
Browse files Browse the repository at this point in the history
  • Loading branch information
YiuChoi committed Apr 21, 2016
1 parent c4676f8 commit 2c197d0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ dependencies {
compile files('libs/AMap_2DMap_V2.8.1_20160202.jar')
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile files('libs/AMap_Search_V3.2.1_20160308.jar')
}
79 changes: 78 additions & 1 deletion app/src/main/java/name/caiyao/fakegps/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
package name.caiyao.fakegps;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AMap.OnMapClickListener, LocationListener {

private MapView mv;
private AMap aMap;
private LatLng latLng;
private SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sharedPreferences = getSharedPreferences("locationHistory", Context.MODE_PRIVATE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mv = (MapView) findViewById(R.id.mv);
assert mv != null;
mv.onCreate(savedInstanceState);
aMap = mv.getMap();
double lat = Double.parseDouble(sharedPreferences.getString("lat", "0.0"));
double lon = Double.parseDouble(sharedPreferences.getString("lon", "0.0"));
if (lat != 0.0 && lon != 0.0){
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat, lon)));
aMap.moveCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel()));
}
aMap.setMapType(AMap.MAP_TYPE_NORMAL);
aMap.setOnMapClickListener(this);
}
Expand Down Expand Up @@ -79,6 +98,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "请点击地图选择一个地点!", Toast.LENGTH_SHORT).show();
return true;
}
sharedPreferences.edit().putString("lat", String.valueOf(latLng.latitude)).putString("lon", String.valueOf(latLng.longitude)).apply();
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
String mockProviderName = LocationManager.GPS_PROVIDER;
Expand All @@ -102,6 +122,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.stop:
startService(new Intent(MainActivity.this, MockGpsService.class).putExtra("action", MockGpsService.ACTION_STOP));
break;
case R.id.search:
View view = LayoutInflater.from(this).inflate(R.layout.dialog_search, null, false);
final EditText et_key = (EditText) view.findViewById(R.id.key);
new AlertDialog.Builder(this).setView(view)
.setTitle("搜索位置")
.setPositiveButton("搜索", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
search(et_key.getText().toString());
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
break;
case R.id.donate:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://qr.alipay.com/apoy1zw1o2xpc7915d")));
break;
Expand All @@ -119,6 +156,46 @@ protected void onPause() {
mv.onPause();
}

private void search(final String key) {
PoiSearch.Query query = new PoiSearch.Query(key, null, null);
query.setPageSize(10);
query.setPageNum(0);
PoiSearch poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult poiResult, int i) {
if (i == 1000) {
final ArrayList<PoiItem> poiItems = poiResult.getPois();
if (poiItems.size() != 0) {
String[] keyList = new String[poiItems.size()];
for (int j = 0; j < poiItems.size(); j++) {
keyList[j] = poiItems.get(j).getTitle();
}
new AlertDialog.Builder(MainActivity.this)
.setTitle("选择位置")
.setSingleChoiceItems(keyList, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(poiItems.get(which).getLatLonPoint().getLatitude(), poiItems.get(which).getLatLonPoint().getLongitude())));
aMap.moveCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel()));
dialog.dismiss();
}
}).show();
} else {
Toast.makeText(MainActivity.this, "没有搜索结果", Toast.LENGTH_SHORT).show();
}

}
}

@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {

}
});
poiSearch.searchPOIAsyn();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/dialog_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/key"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
tools:ignore="LabelFor">

</EditText>
8 changes: 6 additions & 2 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
android:title="@string/stop"
app:showAsAction="always"/>

<item
android:id="@+id/search"
android:title="搜索"
app:showAsAction="never"/>

<item
android:id="@+id/donate"
android:title="@string/donate"
app:showAsAction="never"
/>
app:showAsAction="never"/>
<item
android:id="@+id/about"
android:title="@string/about"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-beta1'
classpath 'com.android.tools.build:gradle:2.1.0-beta3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 2c197d0

Please sign in to comment.