- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
据我所知,Google 不允许开发人员自定义地点选择器布局,
所以我想制作一个如下图所示的地点选择器。
它来自deliveroo app,我使用了map api,但它并不完全像这张照片。
这个选择器就像是定制的地方选择器 api。
这是我的代码:
public class Map extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LatLng markerLatLong;
private static final int REQUEST_CODE_LOCATION_ENABLE = 1;
private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 2;
private static String TAG = Map.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setToolbar();
setFragment();
}
public void setFragment() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void setToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
onSearchPlacesClicked(toolbar);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_LOCATION_ENABLE) {
if (resultCode == RESULT_OK) {
Log.e(TAG, "location enabled");
setLocation();
}
}else if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.e(TAG, "Place: " + place.getName());
setPlace(place.getLatLng());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
setPlace(latLng);
}
});
Button b = (Button) findViewById(R.id.pick_place);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (markerLatLong != null) {
addAddressToPref(-1, markerLatLong.latitude, markerLatLong.longitude);
startActivity(new Intent(Map.this, StoreList.class));
Map.this.finish();
} else {
Toast.makeText(Map.this, R.string.no_place_selected, Toast.LENGTH_SHORT).show();
}
}
});
setLocation();
}
public void addAddressToPref(long id, double latitude, double longitude) {
Singleton.getInstance().setAddressPref(id, latitude, longitude);
}
public void setPlace(final LatLng latLng) {
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
// Remove listener to prevent position reset on camera move.
mMap.setOnCameraChangeListener(null);
}
});
markerLatLong = latLng;
mMap.clear();
mMap.addMarker(new MarkerOptions().position(latLng).title(getString(R.string.my_location)));
}
public void setLocation() {
AppLocationService gps = new AppLocationService(this);
// check if GPS enabled
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
getLocation(latitude, longitude);
// \n is for new line
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
buildAlertMessageNoGps();
}
}
public void getLocation(double latitude, double longitude) {
final CoordinatorLayout rootLayout = (CoordinatorLayout) findViewById(R.id.root_view);
if (latitude != 0 && longitude != 0) {
LatLng latLng = new LatLng(latitude, longitude);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(cameraUpdate);
} else {
Snackbar.make(rootLayout, getResources().getString(R.string.location_problem),
Snackbar.LENGTH_INDEFINITE).setAction("سعی دوباره", new View.OnClickListener() {
@Override
public void onClick(View view) {
setLocation();
}
}).show();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.turn_on_location_text))
.setCancelable(false)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_CODE_LOCATION_ENABLE);
}
})
.setNegativeButton(getString(R.string.exit), new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
Map.this.finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
public void onSearchPlacesClicked(Toolbar toolbar) {
ImageView searchPlace = (ImageView) toolbar.findViewById(R.id.search_place);
searchPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
autoCompleteMethod();
}
});
}
public void autoCompleteMethod() {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
@Override
protected void onResume() {
super.onResume();
if (mMap != null) {
setLocation();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
@Override
public void onBackPressed() {
startActivity(new Intent(Map.this, StoreList.class));
Map.this.finish();
}
}
最佳答案
为 map 创建 Activity 并在您想要定位的地方调用它:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Activity.InsertMapsActivity">
<Button
android:layout_width="wrap_content"
android:id="@+id/btn_set_garage_location"
android:text="set location"
android:layout_height="wrap_content"/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
java代码:
public class InsertMapsActivity extends FragmentActivity implements LocationListener
{
private GoogleMap googleMap;
private LatLng garageLocation;
Button btnSetGarageLocation;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_maps);
btnSetGarageLocation = (Button) findViewById(R.id.btn_set_garage_location);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
if (status != ConnectionResult.SUCCESS)
{ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}
else
{ // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
// TODO: Consider calling
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 100, 0, this);
btnSetGarageLocation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = getIntent();
intent.putExtra("location_lat",garageLocation.latitude);
intent.putExtra("location_lng",garageLocation.longitude);
setResult(RESULT_OK,intent);
finish();
}
});
}
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
@Override
public void onMapClick(LatLng latLng)
{
googleMap.clear();
googleMap.addMarker(new MarkerOptions().position(latLng).title("Custom location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
garageLocation = latLng;
}
});
}
@Override
public void onLocationChanged(Location location)
{
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
googleMap.getMaxZoomLevel();
// Setting latitude and longitude in the TextView tv_location
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onProviderDisabled(String provider)
{
}
}
Activity 中:
private int MAP = 2;
Intent intent = new Intent(getApplicationContext(),InsertMapsActivity.class);
startActivityForResult(intent, MAP);
您的 Activity 结束:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
ImageView garageImage = new ImageView(this);
if (resultCode == Activity.RESULT_OK)
{
}
if(requestCode == MAP)
{
double lat = (double) data.getExtras().get("location_lat");
double lng = (double) data.getExtras().get("location_lng");
}
}
关于android - 如何为 Android 制作自定义地点选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449891/
我想在一个页面上做一个按钮,可以在同一页面调用一个JS函数。该函数将需要创建(打开)新窗口,其 HTML 代码由 JS 函数本身提供。我该怎么做? 这样做的目的是从特定页面生成一个打印友好的页面。 请
我一直在用 php 开发这个项目。该项目的一半是使用 mysql_query 完成的,最新的模块是使用 mysqli 制作的。有很多模块,我不想更改代码。如果是这样的话会不会产生问题。或者我应该将其全
我安装了好几次 qt creator,但它从来没有像我现在的 PC 那样花钱;首先,我使用我的 Pendrive(Qt 5.8 的)上一直有的安装程序,告诉我我无法下载一些存储库,我下载了相同安装程序
我安装了 Qt Creator 5.10.1,当我构建项目时出现错误:“无法确定要运行哪个”make“命令。检查构建配置中的”make“步骤。”。 我已经在另一台 PC 上安装了 Qt,我看到了这个问
看看这个 makefile,它有某种原始的进度指示(可能是一个进度条)。 请给我建议/意见! # BUILD 最初是未定义的 ifndef 构建 # max 等于 256 个 x 十六:= x x x
这个问题会有点长,对此我很抱歉:) 我花了几天时间寻找最好的解决方案,以在 asp mvc 和 JQuery 中制作图像库。 主要问题是当用户点击拇指时显示图像。 我想让整个浏览器 View 变成黑色
我是Python方面的 super 高手。我一直在努力寻找适当的解决方案。这是列表,L = [0, 0, 0, 3, 4, 5, 6, 0, 0, 0, 0, 11, 12, 13, 14, 0, 0
让我们考虑两个简化的 CMakeLists.txt set(GTEST "/usr/local/lib/libgtest.a") set(GMOCK "/usr/local/lib/libgmock.
我如何制作 Makefile,因为这是按源代码分发程序的最佳方式。请记住,这是针对 C++ 程序的,而我是从 C 开发领域开始的。但是可以为我的 Python 程序制作 Makefile 吗? 最佳答
由于 Ord 是 Eq 的子类,我发现很难理解创建该类的新类型实例的样子。 我已经设法做到了: newtype NT1 = NT1 Integer instance Eq NT1 wh
在 PowerShell 中,我想编写一个函数,它接受不同的选项作为参数。没关系,如果它接收多个参数,但它必须接收至少一个参数。我想通过参数定义而不是之后的代码来强制执行它。我可以使用以下代码让它工作
我正在通过构建包使用 enable-ssl 在 heroku (ubuntu) 上安装 ffmpeg。我能够一直构建到这些错误: install: cannot create regular file
我是 FFmpeg 的新手,但作为一个学习一些 mysql 数据库的项目,我正在尝试创建一个视频上传网站。 当我尝试使用此代码制作缩略图时: shell_exec("/usr/local/bin/ff
我想要一个绘制可绘制对象的 Actor ,但将其剪辑为 Actor 的大小。我从 Widget 派生这个类,并使用一些硬编码的值作为一个简单的测试: public class MyWidget ext
我一直在查看 Faxien+Sinan 和 Rebar,Erlang OTP 的基本理念似乎是,在单个 Erlang 镜像实例上安装应用程序和版本。保持发布自包含的最佳实践是什么?有没有办法打包发布,
我正在尝试克隆存储库,但它应该是彼此独立的副本。这背后有什么魔法吗,或者只是使用 svn 客户端并克隆它? 谢谢 最佳答案 试试 svnadmin hotcopy .您可以在 repo mainten
我想做一个这样的菜单: Item 1 Item 2 Item 3 Subitem 1 Subitem 2 但我得到了这个:
为 Yii 创建扩展的最佳方式是什么? 这是我到目前为止所做的 我希望它可以通过 composer 安装,所以我为它创建了一个 github repo。 我在文件夹 vendor/githubname
我尝试制作一个ActionListener,但它给了我一个错误。我导入了事件,但它仍然不起作用。这是我的代码: send.addActionListener(new jj); private clas
我需要能够将 div 内的 HTML 代码恢复为页面就绪状态。我需要这个,因为我想在页面准备好后对 HTML 代码进行一些更改,然后在需要时将其恢复到页面准备好时的状态.. 我想使用克隆,但是如何只复
我是一名优秀的程序员,十分优秀!