当前位置: 首页 > 知识库问答 >
问题:

当我从短信接收经度和纬度时,如何在google map中添加标记

郭阳泽
2023-03-14
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);

            }

        }


    }

共有1个答案

商同化
2023-03-14

使用与地图活动连接的广播接收器....

创建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活动“on mapready”中注册本地广播接收器:

  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);
 类似资料:
  • 我基本上想把gsm模块作为短信发送的经纬度坐标提取到一部Android智能手机上…… 我写了一个简单的应用程序,它以纬度和经度为输入,并使用app Inventor在地图上显示 当我收到来自特定号码的短信时,我想基本上复制提取的纬度和经度自动到我的android应用程序的lat和long字段 我怎么才能做到这一点....

  • 问题内容: 我有一个类型为(com.vividsolutions.jts.geom.Geometry)的几何对象。它目前是经度,纬度形式,我想翻转坐标,使其经度为纬度,这样我就可以将其以GeoJSON格式用于mongodb。 我看到的约束是:a)我想翻转坐标的输入是Geometry对象。b)几何对象将是多边形类型或多多边形。c)我想在将类型强制转换为Polygon / multipolygon之前

  • 问题内容: 我有一个类型为(com.vividsolutions.jts.geom.Geometry)的几何对象。它目前是经度,纬度形式,我想翻转坐标,使其经度为纬度,这样我就可以将其以GeoJSON格式用于mongodb。 我看到的约束是:a)我想翻转坐标的输入是Geometry对象。b)几何对象将是多边形类型或多多边形。c)我想在将类型强制转换为Polygon / multipolygon之前

  • 问题内容: 我在这里阅读这个问题: 在SQL数据库中存储经度和纬度数据时应使用哪种数据类型? 似乎普遍的共识是使用Decimal(9,6)是可行的方法。我的问题是,我真的需要多少精度? 例如,Google的API返回如下结果: 在-122.0841430中,我需要几位数?我已经阅读了几本指南,但是我从中没有足够的道理来解决这个问题。 为了更精确地回答我的问题:如果我想在准确位置的50英尺范围内保持

  • 问题内容: 我想使用Swift获取位置的当前经度和纬度,并通过标签显示它们。我尝试执行此操作,但标签上没有任何显示。 问题答案: 恕我直言,当您寻找的解决方案非常简单时,您就使代码复杂化了。 我已经通过使用以下代码来做到这一点: 首先创建和请求授权的实例 然后检查用户是否允许授权。 用它来做到这一点 您将它们设置为的想法是正确的,但是我能想到的唯一原因是用户未给予您许可,这就是为什么您当前的位置数

  • 问题内容: 在询问具体的代码示例之前,我只想问一下是否可以进行类似此伪代码的查询: 从表中选择项目,其中经度/经度=-在某个经度/经度点的x英里内- 那可行吗?还是我必须跳过一些箍?可以推荐的任何好的方法都很棒! 问题答案: 您应该搜索Haversine公式,但是一个好的开始可能是: 使用PHP,MySQL和Google Maps创建商店定位器 -请参见“使用MySQL查找位置”部分 使用MySQ