Anda di halaman 1dari 6

package com.example.ygwx.

gps_trial_3;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.TextView;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,


LocationListener {
final static int PERMISSION_ALL = 1;
final static String[] PERMISSIONS =
{android.Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
private GoogleMap mMap;
MarkerOptions mo;
Marker marker;
LatLng myCoordinates;
LocationManager locationManager;

static String MQTTHOST = "tcp://m10.cloudmqtt.com:11776";


static String USERNAME = "xoiihasx";
static String PASSWORD = "0FJGv4i9u9kR";

MqttAndroidClient client;
MqttConnectOptions options;
TextView subText;
String topicStr = "Location";
String msg,temp,idx,stat;
String templat,templong,tempidx,tempstat;
float lat,lon,flat,flon;

int naik=9;

public void setLat(float latx){


this.lat = latx;
}

public float getLat(){


return this.lat;
}

public void setLon(float lonx){


this.lon = lonx;
}

public float getLon(){


return this.lon;
}

public void setidx(String idxx){


this.idx = idxx;
}

public String getidx(){


return this.idx;
}

public void setStat(String statx){


this.stat = statx;
}

public String getStat(){


return this.stat;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST
,clientId);

options = new MqttConnectOptions();


options.setUserName(USERNAME);
options.setPassword(PASSWORD.toCharArray());
try {
IMqttToken token = client.connect(options);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {

Toast.makeText(MapsActivity.this,"Connected!",Toast.LENGTH_LONG).show();
setSub();
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable
exception) {
Toast.makeText(MapsActivity.this,"Connection
Failed",Toast.LENGTH_LONG).show();
}
});
}catch (Exception e){
e.printStackTrace();
}

client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {

@Override
public void messageArrived(String topic, MqttMessage message) throws
Exception {
msg=new String(message.toString());
templat=msg.substring(0,10);
flat = Float.parseFloat(templat);
setLat(flat);
templong=msg.substring(11,23);
flon = Float.parseFloat(templong);
setLon(flon);
/*
//Lokasi Terlarang Mangga 2
float latA = (float)-6.97123;
float lonA = (float)107.6345;
float latB = (float)-6.97126;
float lonB = (float)107.63522;
float latC = (float)-6.97208;
float lonC = (float)107.63444;
float latD = (float)-6.97211;
float lonD = (float)107.6352;
*/

//Lokasi Terlarang
float latA = (float)-6.972450;
float lonA = (float)107.639410;
float latB = (float)-6.972502;
float lonB = (float)107.640109;
float latC = (float)-6.972763;
float lonC = (float)107.639381;
float latD = (float)-6.972812;
float lonD = (float)107.640094;

//Lokasi Terlarang
float latAx = (float)-6.972450;
float lonAx = (float)107.639410;
float latBx = (float)-6.972502;
float lonBx = (float)107.640109;
float latCx = (float)-6.972763;
float lonCx = (float)107.639381;
float latDx = (float)-6.972812;
float lonDx = (float)107.640094;

myCoordinates = new LatLng(getLat(), getLon());


marker.setPosition(myCoordinates);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myCoordinates,
17.0f));

if(
((flat < latA) && (flon > lonA)) &&
((flat < latB) && (flon < lonB)) &&
((flat > latC) && (flon > lonC)) &&
((flat > latD) && (flon < lonD))){

AlertDialog.Builder builder = new AlertDialog.Builder(


MapsActivity.this);
builder.setTitle("Notification");
builder.setMessage("Student gets off from the bus at here");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {

}
});
builder.show();

}else{
String forb = "Motor Tidak Berada di Area Terlarang";
Toast.makeText(MapsActivity.this, forb,
Toast.LENGTH_LONG).show();
}
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {

}
});

mo = new MarkerOptions().position(new LatLng(-6.922055,


107.607076)).title("My Current Location");
if (Build.VERSION.SDK_INT >= 23 && !isPermissionGranted()) {
requestPermissions(PERMISSIONS, PERMISSION_ALL);
} else requestLocation();
if (!isLocationEnabled())
showAlert(1);
}

private void setSub(){


try{
client.subscribe(topicStr,0);
}catch (MqttException e) {
e.printStackTrace();
}
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
marker = mMap.addMarker(mo);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-6.922055,
107.607076),17.0f));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(new
LatLng(getwLat(),getLon())));
}

@Override
public void onLocationChanged(Location location) {
//myCoordinates = new LatLng(getLat(), getLon());
marker.setPosition(myCoordinates);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myCoordinates,17.0f));

//Toast.makeText(MapsActivity.this,Float.toString(getLat()),Toast.LENGTH_LONG).show
();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

@Override
public void onProviderEnabled(String s) {

@Override
public void onProviderDisabled(String s) {

}
private void requestLocation() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 10000, 10, this);
}
private boolean isLocationEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

private boolean isPermissionGranted() {


if (checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED ||
checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Log.v("mylog", "Permission is granted");
return true;
} else {
Log.v("mylog", "Permission not granted");
return false;
}
}
private void showAlert(final int status) {
String message, title, btnText;
if (status == 1) {
message = "Your Locations Settings is set to 'Off'.\nPlease Enable
Location to " +
"use this app";
title = "Enable Location";
btnText = "Location Settings";
} else {
message = "Please allow this app to access location!";
title = "Permission access";
btnText = "Grant";
}
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
dialog.setTitle(title)
.setMessage(message)
.setPositiveButton(btnText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int
paramInt) {
if (status == 1) {
Intent myIntent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
} else
requestPermissions(PERMISSIONS, PERMISSION_ALL);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface paramDialogInterface, int
paramInt) {
finish();
}
});
dialog.show();
}
}

Anda mungkin juga menyukai