gpt4 book ai didi

java - 当我从短信收到经度和纬度时,如何在谷歌地图中添加标记

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:07 25 4
gpt4 key购买 nike

所以基本上我正在制作一个应用程序,在其中我会收到一条短信,其中包含纬度和经度,我需要在 map 上添加新标记。但我无法这样做,因为我无法在 onMapReady 函数之外添加标记。关于如何在 onMapReady 函数之外添加标记有什么建议吗? map Activity 如下包 com.example.rajen.railwaycrackk;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
boolean sms_Perm=true;
private GoogleMap mMap;
boolean mapstart=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, Manifest.permission.RECEIVE_SMS)) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.RECEIVE_SMS}, 1);
} else {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.RECEIVE_SMS}, 1);
}

}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (mMap!=null) {
// GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
// .getMap();
Log.d("1", "entered into truee");

this.mMap.addMarker(new MarkerOptions()
.position(new LatLng(10, 10))
.title("Hello world"));

}
}


@Override
public void onRequestPermissionsResult(int requestcode, String[] permissions, int[] grantResults) {
switch (requestcode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
sms_Perm = true;

}

} else {
Toast.makeText(this, "No permission granted", Toast.LENGTH_SHORT).show();
sms_Perm = false;
}

}

}
}
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(20.5937, 78.9629);
this.mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
this.mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mapstart=true;

SmsReceiver myMapReceiver = new SmsReceiver(googleMap);
IntentFilter filter = new IntentFilter(SmsReceiver.ADD_MARKER);
filter.addCategory(Intent.CATEGORY_DEFAULT);
LocalBroadcastManager.getInstance(this).registerReceiver(myMapReceiver, filter);
}


public String LoadInt(String key){
String savedValue;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
savedValue = sharedPreferences.getString(key,null);
return savedValue;
}
}

我的短信广播接收器如下

package com.example.rajen.railwaycrackk;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.content.SharedPreferences;
import android.os.Bundle;

import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


/**
* Created by rajen on 29-01-2018.
*/

public class SmsReceiver extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();


public static final String ADD_MARKER = "some.unique.string.ADD_MARKER";

private GoogleMap mMap;

public SmsReceiver (GoogleMap mMap) {
this.mMap = mMap;
}

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();

if (intent.getAction().compareTo(ADD_MARKER) == 0) {
double lat = intent.getDoubleExtra("lat", 0);
double lng = intent.getDoubleExtra("lng", 0);
}
// do something with map using lat/lng
try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);


// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: " + senderNum + ", message: " + message, duration);
toast.show();
// LatLng position = new LatLng(20.5937, 78.9629);
// MapsActivity.plotOnMap(position);
} // end for loop
} // bundle is null

Intent broadcastIntent = new Intent();
// get 'lat' and 'lng' from message
broadcastIntent.setAction(SmsReceiver.ADD_MARKER);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("lat", 10);
broadcastIntent.putExtra("lng", 10);
LocalBroadcastManager.getInstance(context.getApplicationContext()).sendBroadcast(broadcastIntent);

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);

}

}


}

最佳答案

使用附加到您的 map Activity 的广播接收器...

创建BroadcastReceiver子类如下:

  public class MapRequestReceiver extends BroadcastReceiver {

public static final String ADD_MARKER = "some.unique.string.ADD_MARKER";

private GoogleMap mMap;

public MapRequestReceiver (GoogleMap mMap) {
this.mMap = mMap;
}

public void onReceive(Context context, Intent intent) {
if (intent.getAction().compareTo(ADD_MARKER) == 0) {
double lat = intent.getDoubleExtra("lat", 0);
double lng = intent.getDoubleExtra("lng", 0);

// do something with map using lat/lng
}
}

}

在您的 map Activity “onMapReady”中注册本地广播接收器:

  public void onMapReady(GoogleMap googleMap) {

// ... other stuff...

// check if it already exists in which case do nothing or
// unregister it first using LocalBroadcastManager.

MapRequestReceiver myMapReceiver = new MapRequestsReceiver(googleMap);
IntentFilter filter = new IntentFilter(MapRequestReceiver.ADD_MARKER);
filter.addCategory(Intent.CATEGORY_DEFAULT);
LocalBroadcastManager.getInstance(this).registerReceiver(myMapReceiver, filter);
}

...以及您接收短信的位置:

  Intent broadcastIntent = new Intent();
// get 'lat' and 'lng' from message
broadcastIntent.setAction(MapRequestsReceiver.ADD_MARKER);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("lat", lat);
broadcastIntent.putExtra("lng", lng);
LocalBroadcastManager.getInstance( getApplicationContext() ).sendBroadcast(broadcastIntent);

您可能会发现您想要做的事情比 ADD_MARKER 更多,因此只需使用更多操作(字符串)更新 MapRequestReceiver 并更新“onReceive”即可。

关于java - 当我从短信收到经度和纬度时,如何在谷歌地图中添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com